I got this code:
And it gets executed as follows : new SendFdToApi().execute();
private class SendFdToApi extends AsyncTask<URL, Integer, Long> {
@Override
protected Long doInBackground(URL... urls) {
Gson gson = new GsonBuilder().create();
String jsonObj = gson.toJson(fdReq);
OkHttpClient client = getUnsafeOkHttpClient();
RequestBody body;
Request request;
MediaType mediaType = MediaType.parse("application/json; charset=UTF-8");
body = RequestBody.create(mediaType, jsonObj);
request = new Request.Builder()
.url("url")
.method("POST", body)
.addHeader("Content-Type", "application/json; charset=UTF-8")
.build();
try {
Response response = client.newCall(request).execute();
//------Get response message here--------
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
return null;
}
}
}
After the request is sent I am expecting a JSON message as a result but I don't know how to get the response from the response
variable which is an okhttp3.Response
type.
This is what my API returns:
return new JsonResult(new { mesagge = "message", status = "S" });