Skip to content

Commit

Permalink
修复在主线程读取string造成的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
tsy12321 committed Dec 23, 2016
1 parent f334feb commit f8d6178
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
### 在app目录下的build.gradle中添加依赖

```gradle
compile 'com.tsy:myokhttp:1.1.0'
compile 'com.tsy:myokhttp:1.1.1'
```

## 1 总体简介
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ dependencies {
testCompile 'junit:junit:4.12'

// compile project(":myokhttp")
compile 'com.tsy:myokhttp:1.1.0'
compile 'com.tsy:myokhttp:1.1.1'
}
2 changes: 1 addition & 1 deletion myokhttp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ publish {
userOrg = 'tangsiyuan' //bintray注册的用户名
groupId = 'com.tsy' //compile引用时的第1部分groupId
artifactId = 'myokhttp' //compile引用时的第2部分项目名
publishVersion = '1.1.0' //compile引用时的第3部分版本号
publishVersion = '1.1.1' //compile引用时的第3部分版本号
desc = 'This is a okhttp3 extend library'
website = 'https://github.com/tsy12321/MyOkHttp'

Expand Down
4 changes: 4 additions & 0 deletions myokhttp/src/main/java/com/tsy/sdk/myokhttp/MyOkHttp.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.tsy.sdk.myokhttp;

import android.os.Handler;
import android.os.Looper;

import com.tsy.sdk.myokhttp.builder.DeleteBuilder;
import com.tsy.sdk.myokhttp.builder.DownloadBuilder;
import com.tsy.sdk.myokhttp.builder.GetBuilder;
Expand All @@ -18,6 +21,7 @@
*/
public class MyOkHttp {
private static OkHttpClient mOkHttpClient;
public static Handler mHandler = new Handler(Looper.getMainLooper());

public OkHttpClient getOkHttpClient() {
return mOkHttpClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.tsy.sdk.myokhttp.callback;

import android.os.Handler;
import android.os.Looper;

import com.tsy.sdk.myokhttp.MyOkHttp;
import com.tsy.sdk.myokhttp.response.IResponseHandler;
import com.tsy.sdk.myokhttp.util.LogUtils;

Expand All @@ -19,8 +17,6 @@ public class MyCallback implements Callback {

private IResponseHandler mResponseHandler;

private static Handler mHandler = new Handler(Looper.getMainLooper());

public MyCallback(IResponseHandler responseHandler) {
mResponseHandler = responseHandler;
}
Expand All @@ -29,7 +25,7 @@ public MyCallback(IResponseHandler responseHandler) {
public void onFailure(Call call, final IOException e) {
LogUtils.e("onFailure", e);

mHandler.post(new Runnable() {
MyOkHttp.mHandler.post(new Runnable() {
@Override
public void run() {
mResponseHandler.onFailure(0, e.toString());
Expand All @@ -38,18 +34,13 @@ public void run() {
}

@Override
public void onResponse(Call call, final Response response) throws IOException {
public void onResponse(Call call, final Response response) {
if(response.isSuccessful()) {
mHandler.post(new Runnable() {
@Override
public void run() {
mResponseHandler.onSuccess(response);
}
});
mResponseHandler.onSuccess(response);
} else {
LogUtils.e("onResponse fail status=" + response.code());

mHandler.post(new Runnable() {
MyOkHttp.mHandler.post(new Runnable() {
@Override
public void run() {
mResponseHandler.onFailure(response.code(), "fail status=" + response.code());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.Gson;
import com.google.gson.internal.$Gson$Types;
import com.tsy.sdk.myokhttp.MyOkHttp;
import com.tsy.sdk.myokhttp.util.LogUtils;

import java.io.IOException;
Expand Down Expand Up @@ -33,7 +34,7 @@ private Type getType() {
}

@Override
public final void onSuccess(Response response) {
public final void onSuccess(final Response response) {
ResponseBody responseBody = response.body();
String responseBodyStr = "";

Expand All @@ -42,19 +43,37 @@ public final void onSuccess(Response response) {
} catch (IOException e) {
e.printStackTrace();
LogUtils.e("onResponse fail read response body");
onFailure(response.code(), "fail read response body");
MyOkHttp.mHandler.post(new Runnable() {
@Override
public void run() {
onFailure(response.code(), "fail read response body");
}
});
return;
} finally {
responseBody.close();
}

final String finalResponseBodyStr = responseBodyStr;

try {
Gson gson = new Gson();
onSuccess(response.code(), (T) gson.fromJson(responseBodyStr, getType()));
MyOkHttp.mHandler.post(new Runnable() {
@Override
public void run() {
Gson gson = new Gson();
onSuccess(response.code(), (T) gson.fromJson(finalResponseBodyStr, getType()));
}
});
} catch (Exception e) {
e.printStackTrace();
LogUtils.e("onResponse fail parse gson, body=" + responseBodyStr);
onFailure(response.code(), "fail parse gson, body=" + responseBodyStr);
LogUtils.e("onResponse fail parse gson, body=" + finalResponseBodyStr);
MyOkHttp.mHandler.post(new Runnable() {
@Override
public void run() {
onFailure(response.code(), "fail parse gson, body=" + finalResponseBodyStr);
}
});

}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tsy.sdk.myokhttp.response;

import com.tsy.sdk.myokhttp.MyOkHttp;
import com.tsy.sdk.myokhttp.util.LogUtils;

import org.json.JSONArray;
Expand All @@ -19,7 +20,7 @@
public abstract class JsonResponseHandler implements IResponseHandler {

@Override
public final void onSuccess(Response response) {
public final void onSuccess(final Response response) {
ResponseBody responseBody = response.body();
String responseBodyStr = "";

Expand All @@ -28,26 +29,54 @@ public final void onSuccess(Response response) {
} catch (IOException e) {
e.printStackTrace();
LogUtils.e("onResponse fail read response body");
onFailure(response.code(), "fail read response body");

MyOkHttp.mHandler.post(new Runnable() {
@Override
public void run() {
onFailure(response.code(), "fail read response body");
}
});
return;
} finally {
responseBody.close();
}

final String finalResponseBodyStr = responseBodyStr;

try {
Object result = new JSONTokener(responseBodyStr).nextValue();
final Object result = new JSONTokener(finalResponseBodyStr).nextValue();
if(result instanceof JSONObject) {
onSuccess(response.code(), (JSONObject) result);
MyOkHttp.mHandler.post(new Runnable() {
@Override
public void run() {
onSuccess(response.code(), (JSONObject) result);
}
});
} else if(result instanceof JSONArray) {
onSuccess(response.code(), (JSONArray) result);
MyOkHttp.mHandler.post(new Runnable() {
@Override
public void run() {
onSuccess(response.code(), (JSONArray) result);
}
});
} else {
LogUtils.e("onResponse fail parse jsonobject, body=" + responseBodyStr);
onFailure(response.code(), "fail parse jsonobject, body=" + responseBodyStr);
LogUtils.e("onResponse fail parse jsonobject, body=" + finalResponseBodyStr);
MyOkHttp.mHandler.post(new Runnable() {
@Override
public void run() {
onFailure(response.code(), "fail parse jsonobject, body=" + finalResponseBodyStr);
}
});
}
} catch (JSONException e) {
e.printStackTrace();
LogUtils.e("onResponse fail parse jsonobject, body=" + responseBodyStr);
onFailure(response.code(), "fail parse jsonobject, body=" + responseBodyStr);
LogUtils.e("onResponse fail parse jsonobject, body=" + finalResponseBodyStr);
MyOkHttp.mHandler.post(new Runnable() {
@Override
public void run() {
onFailure(response.code(), "fail parse jsonobject, body=" + finalResponseBodyStr);
}
});
}
}

Expand All @@ -63,4 +92,4 @@ public void onSuccess(int statusCode, JSONArray response) {
public void onProgress(long currentBytes, long totalBytes) {

}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tsy.sdk.myokhttp.response;

import com.tsy.sdk.myokhttp.MyOkHttp;
import com.tsy.sdk.myokhttp.util.LogUtils;

import java.io.IOException;
Expand All @@ -14,7 +15,7 @@
public abstract class RawResponseHandler implements IResponseHandler {

@Override
public final void onSuccess(Response response) {
public final void onSuccess(final Response response) {
ResponseBody responseBody = response.body();
String responseBodyStr = "";

Expand All @@ -23,13 +24,25 @@ public final void onSuccess(Response response) {
} catch (IOException e) {
e.printStackTrace();
LogUtils.e("onResponse fail read response body");
onFailure(response.code(), "fail read response body");
MyOkHttp.mHandler.post(new Runnable() {
@Override
public void run() {
onFailure(response.code(), "fail read response body");
}
});
return;
} finally {
responseBody.close();
}

onSuccess(response.code(), responseBodyStr);
final String finalResponseBodyStr = responseBodyStr;
MyOkHttp.mHandler.post(new Runnable() {
@Override
public void run() {
onSuccess(response.code(), finalResponseBodyStr);
}
});

}

public abstract void onSuccess(int statusCode, String response);
Expand Down

0 comments on commit f8d6178

Please sign in to comment.