接口类:AsyncHandler.java
package com.hxsmart.intelligentizepos.util;
/**
* Developer : xiongwenwei@aliyun.com
* Create Time :
* Function:
*/
public interface AsyncHandler {
public void onSuccess(String json);
public void onFailure(String error);
}
HttpUtil.java
package com.hxsmart.intelligentizepos.util;
import android.content.Context;
import com.hxsmart.intelligentizepos.application.IntelligentizeApplication;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.BaseJsonHttpResponseHandler;
import org.apache.http.Header;
import org.apache.http.entity.StringEntity;
import org.json.JSONObject;
/**
* Developer : xiongwenwei@aliyun.com
* Create Time :2016-5-23 14:05:26
* Function:网络请求框架AsyncHttp二次封装
*/
public class HttpUtil {
/**
* @param url API接口地址
* @param params 请求参数
* @param handler 请求结果
*/
public void post(Context context,String url, JSONObject params, final AsyncHandler handler) {
if(NetUtil.checkNetworkAvailable(IntelligentizeApplication.getInstance())){
try {
SharedPreferencesUtils msp = new SharedPreferencesUtils(context);
String host = "http://"+msp.get(SharedPreferencesUtils.SVRIP,"")+":"+msp.get(SharedPreferencesUtils.SVRPORT,"")+"/";
LogUtil.i("请求地址", host + url);
LogUtil.i("请求参数", params.toString());
AsyncHttpClient client = new AsyncHttpClient();
client.setTimeout(10 * 1000);//设置超时:10秒
StringEntity entity = new StringEntity(params.toString(), "UTF-8");
String contentType = "application/json";//返回数据类型;charset=utf-8
client.post(IntelligentizeApplication.getInstance(), ApiUtil.HOST + url, entity, contentType, new BaseJsonHttpResponseHandler<String>() {
@Override
public void onSuccess(int i, Header[] headers, String s, String s2) {
try {
handler.onSuccess(s);
LogUtil.i("请求结果", s );
} catch (Exception e) {
handler.onSuccess(null);
e.printStackTrace();
}
}
@Override
public void onFailure(int i, Header[] headers, Throwable throwable, String s, String s2) {
handler.onFailure("网络请求失败!");
LogUtil.i("请求结果", "本次网络请求失败!!!");
}
@Override
protected String parseResponse(String s, boolean b) throws Throwable {
return null;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}else{
LogUtil.i("请求结果","无网络连接!");
ToastUtil.show("无网络连接!");
}
}
}
使用:
HttpUtil httpUtil = new HttpUtil();
httpUtil.post(context,ApiUtil.GetSms, getSmsParams(etPhoneNumber.getText().toString()), new AsyncHandler() {
@Override
public void onSuccess(String json) {
GetSmsBean bean = BeanUtil.getBean(json, GetSmsBean.class);
if (bean.getRetCode().equals("00")) {
//变换布局、发送计时器
includePhone.setVisibility(View.GONE);
includeSmsCode.setVisibility(View.VISIBLE);
Message message = handler.obtainMessage(1);
handler.sendMessageDelayed(message, 0); //发送message
ToastUtil.show(bean.getTip());
} else {
ToastUtil.show("获取短信验证码失败");
}
}
@Override
public void onFailure(String error) {
ToastUtil.show(error);
}
});
网络检测类:NetUtil.java
package com.hxsmart.intelligentizepos.util;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
/**
* Developer : xiongwenwei@aliyun.com
* Create Time :2016-5-23 13:36:53
* Function:检查设备是否有网络
*/
public class NetUtil {
// 检测网络
public static boolean checkNetworkAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
LogUtil.i(null, "couldn't get connectivity manager");
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].isAvailable()) {
// LogUtil.i(null, "network is available");
return true;
}
}
}
}
// LogUtil.i(null, "network is not available");
return false;
}
}
添加权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
JAR包:
android-async-http-1.4.6.jar
httpcore-4.3.2.jar