I\'m using retrofit. To catch response i\'m using Interceptor:
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.interceptors().add(myinterceptor);
>
JSONObject postdata = new JSONObject();
try {
postdata.put("citizenId", "2222222222222");
postdata.put("accuracy", 3043.323);
postdata.put("provider", "wifi");
postdata.put("gpsTime", 1111111111111L);
postdata.put("lat", 23434.564);
postdata.put("lng", 34343.5445);
postdata.put("appId", "201");
} catch(JSONException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
RequestBody body = RequestBody.create(MEDIA_TYPE,postdata.toString());
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
final OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
// final OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
final Request request = new Request.Builder()
.url(base_url)
.post(body)
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJvd25lcklkIjoyLCJvd25lclR5cGUiOiJMRUFERVIiLCJpYXQiOjE1MDE4Mjc4MDMsImV4cCI6MzMwMzc4Mjc4MDMsImF1ZCI6InNlbmRpdC5hc2lhIiwiaXNzIjoic2VsZiJ9.3Gpn3beZfdYsMOLTjksLwmxyfbrfqiojdm1n-gh6CXY")
.addHeader("cache-control", "no-cache")
.build();
client.newCall(request).enqueue(new Callback() {
@SuppressLint("LongLogTag")
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
// response = client.newCall(request).execute();
// Protocol protocol = response.protocol();
// assertEquals(Protocol.HTTP_1_1, protocol);
// BufferedSource source = response.body().source();
// source.request(Long.MAX_VALUE); // Buffer the entire body.
// Buffer buffer = source.buffer();
// String responseBodyString = buffer.clone().readString(Charset.forName("UTF-8"));
if(response.code() == 200) {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("code",200);
jsonObject.put("status","OK");
jsonObject.put("message","Successful");
MediaType contentType = response.body().contentType();
ResponseBody body = ResponseBody.create(contentType, jsonObject.toString());
BufferedSource source = response.body().source();
source.request(Long.MAX_VALUE); // Buffer the entire body.
Buffer buffer = source.buffer();
String responseBodyString = buffer.clone().readString(Charset.forName("UTF-8"));
Log.e("response body responseBodyString ", body.string());
Log.e("response body responseBodyString ", responseBodyString);
} catch (JSONException e) {
e.printStackTrace();
}
Log.e("response", String.valueOf(response));
Log.e("response body", String.valueOf(response.body()));
} }catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call call, IOException e) {
Log.e("response onFailure ", String.valueOf(e));
}
});