public class NetWorkUtils {
private static final String SERVER_INTERFACE ="http://172.16.2.135:8089/";
//局域网: http://172.16.2.135:8089
//远程:http://58.250.30.13:8086/
//后台协商的字段
// private static final String SUCCESS_FLAG_VALUE = "1";
private static final String SUCCESS_FLAG ="success";
private static final String FAIL_FLAG ="message";
//状态值,暴露出去
public static final String NO_NETWORK ="NULL_NETWORK";
private static OkHttpClient mOkHttpClient;
private static NetWorkUtils mNetWorkUtils;
public static NetWorkUtils get() {
if (mNetWorkUtils ==null) {
mNetWorkUtils =new NetWorkUtils();
}
return mNetWorkUtils;
}
private NetWorkUtils() {
mOkHttpClient =new OkHttpClient();
}
public void postAsyncHttp(final Context context, String action, Object data, Map paramsData, final int httpType, final HttpRequestCallback requestCallback) {
if (checkConnect(context)) {
requestCallback.onFailure(NO_NETWORK, httpType);
return;
}
mOkHttpClient.newBuilder()
.readTimeout(20000, TimeUnit.SECONDS)//设置读取超时时间
.writeTimeout(20000, TimeUnit.SECONDS)//设置写的超时时间
.connectTimeout(20000, TimeUnit.SECONDS)//设置连接超时时间
.build();
Request.Builder requestBuilder =new Request.Builder();
requestBuilder.addHeader("Content-Type", "application/json");
String url =SERVER_INTERFACE + action;
if (data !=null) {
if (paramsData !=null) {
boolean init =true;
for (Map.Entryentry :
paramsData.entrySet()) {
if (init) {
url +="?" +entry.getKey() +"=" +entry.getValue();
init =false;
}else {
url +="&" +entry.getKey() +"=" +entry.getValue();
}
}
}
requestBuilder.post(RequestBody.create(
MediaType.parse("application/json; charset=utf-8"),
new Gson().toJson(data)));
Log.d("zc", "json:" +new Gson().toJson(data));
}else if (paramsData !=null) {
FormBody.Builder bodyBuilder =new FormBody.Builder();
for (Map.Entryentry :
paramsData.entrySet()) {
bodyBuilder.add(entry.getKey(), entry.getValue());
}
requestBuilder.post(bodyBuilder.build());
}
Log.d("zc", "url:" +url);
Request request =requestBuilder
.url(url)
.build();
final Call call =mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
requestCallback.onFailure(e.getLocalizedMessage(), httpType);
}
@Override
public void onResponse(Call call, Response response)throws IOException {
setResponse(requestCallback, response.body().string(), httpType);
}
});
}
public void getSyncHttp(Context context, final int httpType, String url, final HttpRequestCallback requestCallback) {
if (checkConnect(context)) {
requestCallback.onFailure(NO_NETWORK, httpType);
return;
}
Request request =new Request
.Builder().url(url).build();
Call call =mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
requestCallback.onFailure(e.getLocalizedMessage(), httpType);
}
@Override
public void onResponse(Call call, Response response)throws IOException {
setResponse(requestCallback, response.body().string(), httpType);
}
});
}
//设置成功回调的数据
private void setResponse(HttpRequestCallback requestCallback, String jsonStr, int httpType) {
try {
JSONObject object =new JSONObject(jsonStr);
if (object.getBoolean(SUCCESS_FLAG)) {
requestCallback.onResponse(jsonStr, httpType);
}else {
requestCallback.onFailure(object.getString(FAIL_FLAG), httpType);
}
}catch (Exception e) {
requestCallback.onFailure(e.toString(), httpType);
}
}
//检查网络状态
private static boolean checkConnect(Context cont) {
ConnectivityManager cm = (ConnectivityManager) cont.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info =cm.getActiveNetworkInfo();
return info ==null;
}
}
//网络接口
public interface HttpRequestCallback {
/**
* 獲取數據成功
*
* @param request
* @param type
*/
void onResponse(String request, int type);
/**
* 獲取數據失敗
*
* @param exp
*/
void onFailure(String exp, int type);
}