1,在没有网络的情况下的处理
相信大家面对这个情况处理起来是毫无压力的.
//有网
if (Utils.isNetworkConnected(this)) {
loadingView.setVisibility(View.VISIBLE);//显示正在加载
//联网获取数据
getDataFromNet();
} else {//没网直接显示本地数据.
showView();
Toast.makeText(this, "离线状态中", Toast.LENGTH_SHORT).show();
}```
离线状态时加载本地数据:
![离线模式.jpg](http://upload-images.jianshu.io/upload_images/1232173-a2d80ab9c11111cd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
##2,有网情况下##
有网的时候也还是能分两种情况的:
一,网速好的情况下,当然加载数据非常顺利,就不赘述了.
二,网速不稳定的情况下:
我遇到的情况是这样的:在网速不稳定的情况下,
a,有时是连接超时(根本就连不上服务器)
b,有时是调用接口返回数据result!=0(即虽然连上服务器了,因为网速很慢,又不稳定,在返回数据的时候网络中断了)
c,**更糟糕的是 偶发性**的一直停留在加载页面中,用户体验很差.
其原因:****
![一直停留在加载页面.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,//获取任务数据的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, "获取服务器最新数据失败", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(StaticData.TAG, "获取数据失败:" + error.toString());
showView();
Toast.makeText(MyTaskActivity.this, "获取服务器最新数据失败", Toast.LENGTH_SHORT).show();
}
}, map);
相信到这里,处理起来都很容易.
难在** c **这种情况,网速很慢,却又稳定,所以在加载界面停留很久.
我的处理方式是
在发送这个请求的同时启动服务定时监测网速变化
定时检测网速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被终止的同时也停止定时器继续运行
timer.cancel();
timer = null;
}
//定时任务
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) {//连续四次检测网速都小于20kb/s 断定网速很差.
isNetBad = true;
Log.i("NetworkSpeedService", "网速差 " + isNetBad);
time = 0; //重新检测
}
if (isNetBad) {
handler.post(new Runnable() {
@Override
public void run() {
receiverIntent.putExtra("is_slow_net_speed", isNetBad);
sendBroadcast(receiverIntent);//发送广播去取消这次请求.
}
});
}
}
}
}
连续四次检测网速都小于20kb/s 断定网速很差,直接取消该请求,进入离线模式