The problem is that i cant get the JSON result from my doInBackground. I know i need a handler and work with onPostExecute, but i cant make it work. I have a controller whos in charge of making the conection extending from AsyncTask. I call that controller.execute() on the main thread and it gives me an AsyncTask object instead of the JSON. This is the controller:
final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
@Override
protected String doInBackground(String... params){
try {
JSONObject newJson = new JSONObject(params.toString());
String dirArchivo = "http://10.0.2.2/SoulStoreProject/"+newJson.get("dir");
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(JSON, newJson.toString());
Request request = new Request.Builder()
.url(dirArchivo)
.post(body)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
} catch (IOException e) {
return e.getMessage().toString();
} catch (JSONException e) {
return e.getMessage().toString();
}
}
and this is the main class:
JSONObject requestObject = new JSONObject();
requestObject.put("nick", txtNicklog.getText().toString());
requestObject.put("password", txtPasslog.getText().toString());
requestObject.put("dir", "devolverJugador.php");
String result = Controller.execute(requestObject);
JSONObject resultFromAsyncTask= new JSONObject(result);
i dont know where to write the handler and how to call it from onPostExecute. Or if there is another way to solve it im open to sugestions