Notice
Recent Posts
Recent Comments
Link
투케이2K
822. (Android/Java) [Http Post Request] AsyncHttpClient (loopj) 라이브러리 사용해 http 요청 시 헤더 값 추가 방법 본문
Android
822. (Android/Java) [Http Post Request] AsyncHttpClient (loopj) 라이브러리 사용해 http 요청 시 헤더 값 추가 방법
투케이2K 2024. 8. 1. 20:50[개발 환경 설정]
개발 툴 : AndroidStudio
개발 언어 : Java / Kotlin
[소스 코드]
try {
// --------------------------------------------------------
// TODO [AsyncHttpClient 라이브러리 버전] : implementation 'com.loopj.android:android-async-http:1.4.9'
// --------------------------------------------------------
// --------------------------------------------------------
// TODO [URL 정의]
// --------------------------------------------------------
String url = "https://jsonplaceholder.typicode.com/posts";
// --------------------------------------------------------
// TODO [AsyncHttpClient 객체 정의 및 타임 아웃 시간 설정]
// --------------------------------------------------------
AsyncHttpClient client = new AsyncHttpClient();
client.setTimeout(5000);
// --------------------------------------------------------
// TODO [request 요청 헤더 요청 값 정의]
// --------------------------------------------------------
String contentType = "application/json";
client.addHeader("Content-type", contentType); // TODO [헤더 값 추가]
client.addHeader("Authorization", "Bearer"); // TODO [헤더 값 추가]
// --------------------------------------------------------
// TODO [Body Json 전송값 정의]
// --------------------------------------------------------
String jsonString = "{\"userId\":1, \"id\":1}";
// --------------------------------------------------------
// TODO [StringEntity 객체 정의]
// --------------------------------------------------------
StringEntity entity = new StringEntity(String.valueOf(jsonString));
// --------------------------------------------------------
// TODO [Request 요청 수행]
// --------------------------------------------------------
client.post(A_Intro.this, url, entity, contentType, new AsyncHttpResponseHandler() {
//TODO [웹요청 시작]
@Override
public void onStart() {
S_Log._F_(A_Intro.this, "AsyncHttpClient :: HTTP 통신 POST [요청] 수행", new String[]{
"URL : " + String.valueOf(url),
"DATA : " + String.valueOf(jsonString)
});
}
//TODO [웹요청 성공]
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
String responseData = String.valueOf(new String(response));
S_Log._F_(A_Intro.this, "AsyncHttpClient :: HTTP 통신 POST [응답] 확인", new String[]{
"URL : " + String.valueOf(url),
"STATUS_CODE : " + String.valueOf(statusCode),
"DATA : " + String.valueOf(responseData)
});
}
//TODO [웹요청 실패]
@Override
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable throwable) {
S_Log._F_(A_Intro.this, "AsyncHttpClient :: HTTP 통신 POST [에러] 발생", new String[]{
"URL : " + String.valueOf(url),
"STATUS_CODE : " + String.valueOf(statusCode),
"ERROR : " + String.valueOf(throwable.getMessage())
});
}
});
}
catch (Exception e){
e.printStackTrace();
}
[결과 출력]
W///===========//: ================================================
I/: [LOG :: CLASS PLACE :: com.example.javaproject.A_Intro$1$1.onStart(A_Intro.java:382)]
I/: ----------------------------------------------------
I/: [LOG :: NOW TIME :: 2024-08-01 13:35:03 목요일]
I/: ----------------------------------------------------
I/: [LOG :: DESCRIPTION :: AsyncHttpClient :: HTTP 통신 POST [요청] 수행]
I/: ----------------------------------------------------
I/: [LOG :: URL : https://jsonplaceholder.typicode.com/posts]
I/: ----------------------------------------------------
I/: [LOG :: DATA : {"userId":1, "id":1}]
W///===========//: ================================================
W///===========//: ================================================
I/: [LOG :: CLASS PLACE :: com.example.javaproject.A_Intro$1$1.onSuccess(A_Intro.java:392)]
I/: ----------------------------------------------------
I/: [LOG :: NOW TIME :: 2024-08-01 13:35:04 목요일]
I/: ----------------------------------------------------
I/: [LOG :: DESCRIPTION :: AsyncHttpClient :: HTTP 통신 POST [응답] 확인]
I/: ----------------------------------------------------
I/: [LOG :: URL : https://jsonplaceholder.typicode.com/posts]
I/: ----------------------------------------------------
I/: [LOG :: STATUS_CODE : 201]
I/: ----------------------------------------------------
I/: [LOG :: DATA : {
"userId": 1,
"id": 101
}]
W///===========//: ================================================
반응형
'Android' 카테고리의 다른 글
Comments