Skip to content

Commit 16109d4

Browse files
committed
fix #45 某些网络环境下,json解释错误
1 parent 51d1b6c commit 16109d4

File tree

3 files changed

+49
-40
lines changed

3 files changed

+49
-40
lines changed

app/src/main/java/io/github/emanual/app/api/RestClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ private static void initClient() {
6666
if (context != null)
6767
client.setCookieStore(new PersistentCookieStore(context));
6868
client.setTimeout(HTTP_Timeout);
69+
client.setEnableRedirects(false);
6970
}
7071

7172
/**

app/src/main/java/io/github/emanual/app/entity/NewsFeedsObject.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package io.github.emanual.app.entity;
22

3+
import com.google.gson.Gson;
4+
import com.google.gson.reflect.TypeToken;
5+
36
import java.io.Serializable;
47
import java.lang.reflect.Type;
58
import java.util.List;
69

7-
import com.google.gson.Gson;
8-
import com.google.gson.reflect.TypeToken;
9-
1010
public class NewsFeedsObject implements Serializable {
1111
private String path;
1212
private String name;
@@ -17,10 +17,18 @@ public static NewsFeedsObject create(String json) {
1717
return new Gson().fromJson(json, NewsFeedsObject.class);
1818
}
1919

20-
public static List<NewsFeedsObject> createNewsFeedsObjects(String json) {
20+
public static List<NewsFeedsObject> createNewsFeedsObjects(String json) throws Exception{
2121
Type collectionType = new TypeToken<List<NewsFeedsObject>>() {
2222
}.getType();
23-
return new Gson().fromJson(json, collectionType);
23+
List<NewsFeedsObject> result = null;
24+
25+
try{
26+
//json格式不一定对,应为网络原因
27+
result = new Gson().fromJson(json, collectionType);
28+
return result;
29+
}catch (Exception e){
30+
throw e;
31+
}
2432
}
2533

2634
public String getPath() {

app/src/main/java/io/github/emanual/app/ui/fragment/NewFeeds.java

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,5 @@
11
package io.github.emanual.app.ui.fragment;
22

3-
import io.github.emanual.app.R;
4-
import io.github.emanual.app.api.NewFeedsAPI;
5-
import io.github.emanual.app.api.RestClient;
6-
import io.github.emanual.app.entity.NewsFeedsObject;
7-
import io.github.emanual.app.ui.Detail;
8-
import io.github.emanual.app.ui.adapter.NewFeedsAdapter;
9-
import io.github.emanual.app.utils.EManualUtils;
10-
import io.github.emanual.app.utils.SwipeRefreshLayoutUtils;
11-
import io.github.emanual.app.utils.UmengAnalytics;
12-
13-
import java.util.ArrayList;
14-
import java.util.HashMap;
15-
import java.util.List;
16-
import java.util.Map;
17-
18-
import org.apache.http.Header;
19-
203
import android.content.Intent;
214
import android.os.Bundle;
225
import android.support.v4.widget.SwipeRefreshLayout;
@@ -31,12 +14,28 @@
3114
import android.widget.AdapterView.OnItemClickListener;
3215
import android.widget.ListView;
3316

34-
import butterknife.ButterKnife;
35-
import butterknife.InjectView;
36-
3717
import com.loopj.android.http.AsyncHttpResponseHandler;
3818
import com.umeng.analytics.MobclickAgent;
3919

20+
import org.apache.http.Header;
21+
22+
import java.util.ArrayList;
23+
import java.util.HashMap;
24+
import java.util.List;
25+
import java.util.Map;
26+
27+
import butterknife.ButterKnife;
28+
import butterknife.InjectView;
29+
import io.github.emanual.app.R;
30+
import io.github.emanual.app.api.NewFeedsAPI;
31+
import io.github.emanual.app.api.RestClient;
32+
import io.github.emanual.app.entity.NewsFeedsObject;
33+
import io.github.emanual.app.ui.Detail;
34+
import io.github.emanual.app.ui.adapter.NewFeedsAdapter;
35+
import io.github.emanual.app.utils.EManualUtils;
36+
import io.github.emanual.app.utils.SwipeRefreshLayoutUtils;
37+
import io.github.emanual.app.utils.UmengAnalytics;
38+
4039
public class NewFeeds extends BaseFragment implements OnRefreshListener {
4140
@InjectView(R.id.lv_newfeeds) ListView lv;
4241
@InjectView(R.id.swipeRefreshLayout) SwipeRefreshLayout swipeRefreshLayout;
@@ -144,20 +143,23 @@ public MyAsyncHttpResponseHandler(int mPage) {
144143
}
145144

146145
@Override public void onSuccess(int statusCode, Header[] headers, byte[] response) {
147-
if (mPage == 1) {
148-
//refresh
146+
try{
149147
List<NewsFeedsObject> names = NewsFeedsObject.createNewsFeedsObjects(new String(response));
150-
data.clear();
151-
data.addAll(names);
152-
adapter.notifyDataSetChanged();
153-
page = mPage;
154-
hasMore = true;
155-
} else {
156-
//loadmore
157-
List<NewsFeedsObject> names = NewsFeedsObject.createNewsFeedsObjects(new String(response));
158-
data.addAll(names);
159-
adapter.notifyDataSetChanged();
160-
page = mPage;
148+
if (mPage == 1) {
149+
//refresh
150+
data.clear();
151+
data.addAll(names);
152+
adapter.notifyDataSetChanged();
153+
page = mPage;
154+
hasMore = true;
155+
} else {
156+
//loadmore
157+
data.addAll(names);
158+
adapter.notifyDataSetChanged();
159+
page = mPage;
160+
}
161+
}catch (Exception e){
162+
toast("哎呀,网络异常!");
161163
}
162164
}
163165

@@ -176,6 +178,4 @@ public MyAsyncHttpResponseHandler(int mPage) {
176178
SwipeRefreshLayoutUtils.setRefreshing(swipeRefreshLayout, false);
177179
}
178180
}
179-
180-
181181
}

0 commit comments

Comments
 (0)