我正在使用okhttp创建一个网络util,它成功获取请求,记录返回数据。但是,如果我尝试将返回字符串解析为json,则会抛出NetworkOnMainThreadException。
HttpUtil类
public class HttpUtil {
private OkHttpClient client;
private Request.Builder builder;
public void get(String url, HttpCallback cb) {
call("GET", url, cb);
}
public void post(String url, HttpCallback cb) {
call("POST", url, cb);
}
private void call(String method, String url, final HttpCallback cb) {
client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
client.newCall(request).enqueue(new Callback() {
Handler mainHandler = new Handler(Looper.getMainLooper());
@Override
public void onFailure(Request request,final IOException e) {
mainHandler.post(new Runnable() {
@Override
public void run() {
cb.onFailure(null, e);
}
});
}
@Override
public void onResponse(final Response response) throws IOException {
mainHandler.post(new Runnable() {
@Override
public void run() {
if (!response.isSuccessful()) {
cb.onFailure(response, null);
return;
}
cb.onSuccess(response);
}
});
}
});
}
public interface HttpCallback {
/**
* called when the server response was not 2xx or when an exception was thrown in the process
*
* @param response - in case of server error (4xx, 5xx) this contains the server response
* in case of IO exception this is null
* @param throwable - contains the exception. in case of server error (4xx, 5xx) this is null
*/
public void onFailure(Response response, Throwable throwable);
/**
* contains the server response
*
* @param response
*/
public void onSuccess(Response response);
}
}
调用util
的片段new HttpUtil().get(Constant.get_cat_list, new HttpUtil.HttpCallback() {
@Override
public void onFailure(Response response, Throwable throwable) {
Toast.makeText(getActivity(), getResources().getString(R.string.network_err), Toast.LENGTH_LONG).show();
}
@Override
public void onSuccess(Response response) {
items = new ArrayList<Item>();
String data = null;
try {
/* !!!!!!!!!!!!!!!Below line fail!!!!!!!!!!!!!!!!!*/
data = response.body().string();
JSONArray json_array = new JSONArray(data);
for (int i = 0; i < json_array.length(); i++) {
JSONObject object = json_array.getJSONObject(i);
items.add(new Item(object.getString("title_tw"), object.getString("image_url")));
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
gridView.setAdapter(new MyAdapter(getActivity()));
}
});
return v;
}
接收回调时,片段中 data = response.body()。string(); 的异常抛出。我想知道这是否意味着我还需要解析JSON的异步任务? (正如我记得在以前的项目中它不需要)
非常感谢你的帮助。