1,在沒有網(wǎng)絡(luò)的情況下的處理##
相信大家面對這個情況處理起來是毫無壓力的.
//有網(wǎng)
if (Utils.isNetworkConnected(this)) {
loadingView.setVisibility(View.VISIBLE);//顯示正在加載
//聯(lián)網(wǎng)獲取數(shù)據(jù)
getDataFromNet();
} else {//沒網(wǎng)直接顯示本地數(shù)據(jù).
showView();
Toast.makeText(this, "離線狀態(tài)中", Toast.LENGTH_SHORT).show();
}```
離線狀態(tài)時加載本地數(shù)據(jù):
![離線模式.jpg](http://upload-images.jianshu.io/upload_images/1232173-a2d80ab9c11111cd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
##2,有網(wǎng)情況下##
有網(wǎng)的時候也還是能分兩種情況的:
一,網(wǎng)速好的情況下,當(dāng)然加載數(shù)據(jù)非常順利,就不贅述了.
二,網(wǎng)速不穩(wěn)定的情況下:
我遇到的情況是這樣的:在網(wǎng)速不穩(wěn)定的情況下,
a,有時是連接超時(根本就連不上服務(wù)器)
b,有時是調(diào)用接口返回數(shù)據(jù)result!=0(即雖然連上服務(wù)器了,因為網(wǎng)速很慢,又不穩(wěn)定,在返回數(shù)據(jù)的時候網(wǎng)絡(luò)中斷了)
c,**更糟糕的是 偶發(fā)性**的一直停留在加載頁面中,用戶體驗很差.
其原因:****
![一直停留在加載頁面.jpg](http://upload-images.jianshu.io/upload_images/1232173-8535863dbb26c7b1.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
a,b情況都好處理:
```java
NetHttp.getNetData(NetHttp.getMyMessage,//獲取任務(wù)數(shù)據(jù)的url
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (response.optInt("result") == 0) {
dbUtil = DBUtil.getInstance();
myMessageTask = new MyMessageTask();
myMessageTask.execute(response);
} else {
showView();
Toast.makeText(MyTaskActivity.this, "獲取服務(wù)器最新數(shù)據(jù)失敗", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(StaticData.TAG, "獲取數(shù)據(jù)失斞:" + error.toString());
showView();
Toast.makeText(MyTaskActivity.this, "獲取服務(wù)器最新數(shù)據(jù)失敗", Toast.LENGTH_SHORT).show();
}
}, map);
相信到這里,處理起來都很容易.
難在** c **這種情況,網(wǎng)速很慢,卻又穩(wěn)定,所以在加載界面停留很久.
我的處理方式是
在發(fā)送這個請求的同時啟動服務(wù)定時監(jiān)測網(wǎng)速變化
定時檢測網(wǎng)速Service:
public class NetWorkService extends Service {
private Handler handler = new Handler();
private Timer timer;
private long rxtxTotal = 0;
private boolean isNetBad = false;
private int time;
private double rxtxSpeed = 1.0f;
private DecimalFormat showFloatFormat = new DecimalFormat("0.00");
private Intent receiverIntent;
public final static String NET_SPEED_RECEIVER_ACTION = "com.ridgepm.network_speed_action";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (timer == null) {
timer = new Timer();
timer.scheduleAtFixedRate(new RefreshTask(), 0L, (long) 2000);
}
receiverIntent = new Intent();
receiverIntent.setAction(NET_SPEED_RECEIVER_ACTION);
int result = super.onStartCommand(intent, flags, startId);
return result;
}
@Override
public void onDestroy() {
super.onDestroy();
// Service被終止的同時也停止定時器繼續(xù)運行
timer.cancel();
timer = null;
}
//定時任務(wù)
class RefreshTask extends TimerTask {
@Override
public void run() {
isNetBad = false;
long tempSum = TrafficStats.getTotalRxBytes()
+ TrafficStats.getTotalTxBytes();
long rxtxLast = tempSum - rxtxTotal;
double tempSpeed = rxtxLast * 1000 / 2000;
rxtxTotal = tempSum;
if ((tempSpeed / 1024d) < 20 && (rxtxSpeed / 1024d) < 20) {
time += 1;
} else {
time = 0;
}
rxtxSpeed = tempSpeed;
Log.i("NetworkSpeedService", showFloatFormat.format(tempSpeed / 1024d) + "kb/s");
if (time >= 4) {//連續(xù)四次檢測網(wǎng)速都小于20kb/s 斷定網(wǎng)速很差.
isNetBad = true;
Log.i("NetworkSpeedService", "網(wǎng)速差 " + isNetBad);
time = 0; //重新檢測
}
if (isNetBad) {
handler.post(new Runnable() {
@Override
public void run() {
receiverIntent.putExtra("is_slow_net_speed", isNetBad);
sendBroadcast(receiverIntent);//發(fā)送廣播去取消這次請求.
}
});
}
}
}
}
連續(xù)四次檢測網(wǎng)速都小于20kb/s 斷定網(wǎng)速很差,直接取消該請求,進(jìn)入離線模式
檢測網(wǎng)絡(luò)速度Service github下載連接