[안드로이드] okhttp로 POST 요청하는 법
안녕하세요. 개발자 드리머즈입니다.
안드로이드에서 okhttp 사용 시
POST http/https request 및 response 사용 법에 대한 포스트입니다.
출처 : https://stackoverflow.com/questions/23456488/how-to-use-okhttp-to-make-a-post-request
OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormBody.Builder()
.add("message", "Your message")
.build();
Request request = new Request.Builder()
.url("http://www.foo.bar/index.php")
.post(formBody)
.build();
try {
Response response = client.newCall(request).execute();
// Do something with the response.
} catch (IOException e) {
e.printStackTrace();
}
public void updateUserInfo(){
OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormBody.Builder()
.add("userId", userId)
.add("email", email)
.add("nickName", nickname)
.add("thumbnailURL", thumbnailURL)
.build();
Request request = new Request.Builder().url("https://www.test.com/insert_user.php")
.post(formBody)
.build();
client.newCall(request).enqueue(updateUserInfoCallback);
}
private Callback updateUserInfoCallback = new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d("TEST", "ERROR Message : " + e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String responseData = response.body().string();
Log.d("TEST", "responseDatae : " + responseData);
}
};
댓글 영역