Volley联网判断有无网络(1)

时间:2021-06-24 19:45:35

在请求后  进行无网络的问题判断

依赖: dependencies {
     compile 'com.github.Whydontyou:Volleyw:1.01'
}
并且
android {

allprojects {
repositories {
maven { url 'https://www.jitpack.io' }
}
}
}
application:
public class App extends Application{    private static Application mInstance;    private static IRequest sIRequest;    public static IRequest getRequestInstance() {        return sIRequest;    }    @Override    public void onCreate() {        super.onCreate();        mInstance = this;        sIRequest = new IRequest(this);    }}
添加权限:
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />   <uses-permission android:name="android.permission.INTERNET" />
这样就可以使用了
post请求:
RequestParams params = new RequestParams();params.put("参数", 值);
App.getRequestInstance().post("URL", this, params, new RequestListener() {    @Override    public void requestSuccess(String s) {    }    @Override    public void requestError(VolleyError volleyError, String s) {    }    @Override    public void noInternet(VolleyError volleyError, String  s) {
     //无网络    }});
get请求:
App.getRequestInstance().get(MainActivity.this, "url", new RequestListener() {    @Override    public void requestSuccess(String json) {        //请求获取数据    }    @Override    public void requestError(VolleyError e, String error) {        //错误    }    @Override    public void noInternet(VolleyError e, String error) {        //无网络连接    }});
这样就可以成功在进入页面时 知道无网络了