1.build.gradle中添加
compile
2.自定义Application
public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
x.Ext.init(this);
x.Ext.setDebug(BuildConfig.DEBUG);
}
}
3.AndroidManifest.xml文件application节点添加
android:name=".BaseApplication"
4.单例模式Xutils3封装
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;
import java.io.File;
import java.util.Map;
public class XutilsHttp {
private volatile static XutilsHttp instance;
private Handler handler;
private XutilsHttp() {
handler = new Handler(Looper.getMainLooper());
}
/**
* 单利模式
*
* @return
*/
public static XutilsHttp getInstance() {
if (instance == null) {
synchronized (XutilsHttp.class) {
if (instance == null) {
instance = new XutilsHttp();
}
}
}
return instance;
}
/**
* 异步get请求返回结果,json字符串
*
* @param result
* @param callBack
*/
private void onSuccessResponse(final String result, final XCallBack callBack) {
handler.post(new Runnable() {
@Override
public void run() {
if (callBack != null) {
callBack.onResponse(result);
}
}
});
}
private void onFailResponse(final String result, final XCallBack callBack) {
handler.post(new Runnable() {
@Override
public void run() {
if (callBack != null) {
callBack.onFail(result);
}
}
});
}
private void onCancelResponse(final Callback.CancelledException cex, final XCallBack callBack) {
handler.post(new Runnable() {
@Override
public void run() {
if (callBack != null) {
callBack.onCancel(cex);
}
}
});
}
public interface XCallBack {
void onResponse(String result);
void onFail(String result);
void onCancel(Callback.CancelledException cex);
}
public interface XDownLoadCallBack {
void onstart();
void onLoading(long total, long current, boolean isDownloading);
void onSuccess(File result);
void onFail(String result);
void onFinished();
}
/**
* 普通get请求
*
* @param url
*