In android studio project I am using okhttp to send post requests.
public void sentRequest(.... ....) {
RequestBody formBody = new FormBody.Builder().add("number", number).build();
Request request = new Request.Builder().url(serverBase + API_numberAvailabilityCheck).post(formBody).build();
call = client.newCall(request);
new Thread(new Runnable() {
@Override
public void run() {
final HashMap<String, Object> requestResult = new HashMap<>();
try {
final Response response = call.execute();
}
...........
...........
}
}).run();
}
But it throws an NetworkOnMainThreadException
exception on line where I am calling final Response response = call.execute();
despite to new Thread
. Why it's throwing like exception? And how to fix that?