--
public class Httputils {
private static Httputils httputils;
private static OkHttpClient client;
private Httputils() {
client = new OkHttpClient.Builder()
.connectTimeout(5000, TimeUnit.SECONDS)
.readTimeout(5000, TimeUnit.SECONDS)
.addInterceptor(new HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY)
)
.build();
}
public static Httputils getHttputils() {
if (httputils == null) {
synchronized (Httputils.class) {
if (httputils == null) {
httputils = new Httputils();
}
}
}
return httputils;
}
public void get(String path, Callback callback) {
client.newCall(
new Request.Builder()
.url(path)
.get()
.build()
)
.enqueue(callback);
}
public void post(String path, String parames, Callback callback) {
client.newCall(
new Request.Builder()
.url(path)
.post(RequestBody
.create(parames,
MediaType.parse("application/json;charset=utf-8")
)
)
.build()
)
.enqueue(callback);
}
public boolean isPanduan(Context context){
ConnectivityManager con = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = con.getActiveNetworkInfo();
if (info != null && info.isConnected()) return true ;
return false ;
}
}