Android에서 retrofit을 이용해서 spring과 통신하는 테스트를 진행해본다.
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0
<uses-permission android:name="android.permission.INTERNET"/>
<application
...
android:usesCleartextTraffic="true">
package org.smu.blood.data
class User {
var id: String? = null
get(){ return field }
set(value){ field = value }
var password: String? = null
get(){ return field }
set(value){field = value}
}
package org.smu.blood.api
import org.smu.blood.User
import retrofit2.Call
import retrofit2.http.*
public interface API {
// login
@POST("android")
fun getLoginResponse(@Body user: User): Call<String>
}
package org.smu.blood.api
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
object RetrofitBuilder {
var api: API
init{
val retrofit = Retrofit.Builder()
.baseUrl("http://<서버IP>:8090/connection/") // 요청 보내는 API 서버 url. /로 끝나야 함함
.addConverterFactory(GsonConverterFactory.create()) // Gson을 역직렬화
.build()
api = retrofit.create(API::class.java)
}
}
package org.smu.blood
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import com.google.gson.Gson
import org.smu.blood.api.RetrofitBuilder
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
class MainActivity : AppCompatActivity() {
var id: String = ""
var pw: String = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button)
val userId = findViewById<EditText>(R.id.userId)
val userPassword = findViewById<EditText>(R.id.userPassword)
button.setOnClickListener{
id = userId.text.toString()
pw = userPassword.text.toString()
val user = User()
user.id = userId.text.toString()
user.password = userPassword.text.toString()
Log.d("BUTTON CLICKED", "id: " + user.id + ", pw: " + user.password)
Login(user)
}
}
fun Login(user: User){
val call = RetrofitBuilder.api.getLoginResponse(user)
call.enqueue(object : Callback<String> { // 비동기 방식 통신 메소드
override fun onResponse( // 통신에 성공한 경우
call: Call<String>,
response: Response<String>
) {
if(response.isSuccessful()){ // 응답 잘 받은 경우
Log.d("RESPONSE: ", response.body().toString())
}else{
// 통신 성공 but 응답 실패
Log.d("RESPONSE", "FAILURE")
}
}
override fun onFailure(call: Call<String>, t: Throwable) {
// 통신에 실패한 경우
Log.d("CONNECTION FAILURE: ", t.localizedMessage)
}
})
}
}
package com.example.connection;
import org.springframework.data.annotation.Id;
public class User {
@Id
private String id; // _id로 지정
private String password;
public User() {}
public User(String userId, String password) {
this.id = userId;
this.password = password;
}
public String getid() {
return id;
}
public String getpassword() {
return password;
}
public String toString() {
return String.format("User[userId:%s, password: %s]", id, password);
}
}
package com.example.connection;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.example.connection.User;
/**
* Handles requests for the application home page.
*/
@Controller
public class HomeController {
@PostMapping(value="android")
@ResponseBody
public String androidResponse(@RequestBody User user) {
System.out.println("Connection from Android");
System.out.println("id: " + user.getid() + ", pw: " + user.getpassword());
return "1";
}
}
[Spring] 응답 문자열 한글 깨짐 문제 해결 (0) | 2022.02.16 |
---|---|
JWT (JSON Web Token) (0) | 2022.01.28 |
[Spring] Spring, MongoDB 연동 (Spring Data MongoDB) (0) | 2022.01.20 |
[spring] Spring MVC: Model Interface, RequestParam Annotation, Spring MVC form (0) | 2022.01.19 |
[Spring] Spring MVC : Multiple view pages, Multiple Controllers (0) | 2022.01.18 |