Skip to content

Commit

Permalink
MoonOkHttp3修改
Browse files Browse the repository at this point in the history
改正封装的OKHttp出现NetWorkOnMainThreadException的情况,将response.body().string()操作放到子线程中处理。
  • Loading branch information
henrymorgen committed Sep 20, 2017
1 parent 11e16e4 commit fff99eb
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ public void onError(Request request, Exception e) {

}
@Override
public void onResponse(Response response) throws IOException{
String str = response.body().string();
public void onResponse(String str) throws IOException{
Log.d(TAG, str);
Toast.makeText(getApplicationContext(), "请求成功", Toast.LENGTH_SHORT).show();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import okhttp3.Request;
import okhttp3.Response;
public class OkHttpEngine {
private static OkHttpEngine mInstance;
private static volatile OkHttpEngine mInstance;
private OkHttpClient mOkHttpClient;
private Handler mHandler;

Expand Down Expand Up @@ -64,16 +64,16 @@ public void onFailure(Call call, IOException e) {

@Override
public void onResponse(Call call, Response response) throws IOException {
sendSuccessCallback(response, callback);
sendSuccessCallback(response.body().string(), callback);
}

private void sendSuccessCallback(final Response object, final ResultCallback callback) {
private void sendSuccessCallback(final String str, final ResultCallback callback) {
mHandler.post(new Runnable() {
@Override
public void run() {
if (callback != null) {
try {
callback.onResponse(object);
callback.onResponse(str);
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ public abstract class ResultCallback
{
public abstract void onError(Request request, Exception e);

public abstract void onResponse(Response response) throws IOException;
public abstract void onResponse(String str) throws IOException;
}

0 comments on commit fff99eb

Please sign in to comment.