【发布时间】:2016-08-16 14:07:03
【问题描述】:
问题是我无法从我的 doInBackground 获取 JSON 结果。我知道我需要一个处理程序并使用 onPostExecute,但我无法使其工作。我有一个控制器负责从 AsyncTask 扩展连接。我在主线程上调用了 controller.execute(),它给了我一个 AsyncTask 对象而不是 JSON。 这是控制器:
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();
}
}
这是主类:
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);
我不知道在哪里编写处理程序以及如何从 onPostExecute 调用它。或者如果有其他方法可以解决它,我愿意接受建议
【问题讨论】:
标签: android json android-asynctask handler okhttp3