AS 集成 需添加依赖 compile ‘org.xutils:xutils:3.5.0’
最基础的就不多说了 xutils 是一个特稳定的网络请求框架 使用起来也是非常方便的 上传下载速度 以及https 相对来说是一个全面 稳定 好用的第三方库
下面是我自己封装的一个简单网络助手
public class NetHelper {
public static String TypeJson = “application/json”; //String
public static String TypeData = “multipart/form-data”; //file
/**
* 请求头参数
* @param context 上下文
* @param url 网址
* @param Type 类型
* @return
*
*/
public static RequestParams getHeader(Context context,String url,String Type){
sharedPreferences = context.getSharedPreferences("userlogin",Context.MODE_PRIVATE);
appId = sharedPreferences.getString("appid", "");
userId = sharedPreferences.getString("userId", "");
token = sharedPreferences.getString("token", "");
params.addHeader("Content-Type",Type);
params.addHeader("token",token );
params.addHeader("appId",appId );
params.addHeader("userId",userId );
return params;
}
/**
* x get 请求方法
* @param params
* @param context
* @param from
*
*/
public static void XGET(RequestParams params, final Context context, final FromNetDataBack fromNetDataBack, final String from){
x.http().get(params, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
Log.d("tag", "onSuccess: get---"+result);
JSONObject object = JSON.parseObject(result);
String status = object.getString("status");
if (status.equals("1")){
fromNetDataBack.onNetSuccess(result,from);
}else {
String failure = object.getString("failure");
fromNetDataBack.onNetSuccessYiviErrorCode(failure,from);
ErrType.successErrCode(failure,context);
}
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
Log.d("tag", "onError: get---"+ex);
fromNetDataBack.onNetError(ex,from);
ErrType.errType(context,ex);
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
/**
* x post 请求方法
* @param params 请求体
* @param context 上下文
* @param from 请求位置
*/
public static void XPOST(RequestParams params, final Context context, final FromNetDataBack fromNetDataBack, final String from){
x.http().post(params, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
JSONObject object = JSON.parseObject(result);
String status = object.getString("status");
if (status.equals("1")){
fromNetDataBack.onNetSuccess(result,from);
}else {
String failure = object.getString("failure");
fromNetDataBack.onNetSuccessYiviErrorCode(failure,from);
ErrType.successErrCode(failure,context);
}
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
Log.d("tag", "onSuccess: post---"+ex);
fromNetDataBack.onNetError(ex,from);
ErrType.errType(context,ex);
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
//数据返回接口
public interface FromNetDataBack{
/**
* 网络请求成功 正常返回数据
* @param data
* @param from
*/
void onNetSuccess(String data,String from);
/**
* 网络请求成功 错误码
* @param code
* @param from
*/
void onNetSuccessYiviErrorCode(String code,String from);
/**
* 请求失败 网络错误码
* @param ex
* @param from
*/
void onNetError(Throwable ex,String from);
}
//绑定图片方法
public static void XBindImageview(ImageView img,String netUrl){
ImageOptions options = new ImageOptions.Builder().setFailureDrawableId(R.drawable.morentouxiang).setLoadingDrawableId(R.drawable.morentouxiang).build();
x.image().bind(img, BASEURL..IMG_URL+netUrl,options);
}
}
简单封装 更多一些的网络控制以及和ui交互 可根据自己的服务器金额和处理