1、不依赖第三方的网络请求框架;
2、支持get、post、post文件上传;
3、支持重定向;
4、支持线程池的使用(串行线程池,和并行线程池),支持自定义线程池
compile 'com.bean.libraries:xhttp:1.0.3'
JJLogger.debug(true);
XHttp.getInstance()
.get("http://3434343434")
.setTag("aaa")
.setOnXHttpCallback(new OnXHttpCallback() {
@Override
public void onSuccess(Response response) {
Log.i("xxx", "response " +response.toString());
mTextView.setText(response.toString());
}
@Override
public void onFailure(Exception ex, String errorCode) {
Log.i("xxx", "onFailure " +ex.toString());
Log.i("xxx", "onFailure " +errorCode);
}
});
XHttp.getInstance()
.get("http://sdadadadasd")
.setTag("bbb")
.setTimeout(5000)
.setOnXHttpCallback(new OnXHttpCallback() {
@Override
public void onSuccess(final Response response) {
mImageView.setImageBitmap(response.toBitmap());
Log.i("xxx", "onSuccess" );
}
@Override
public void onFailure(Exception ex, String errorCode) {
Log.i("xxx", "onFailure " +ex.toString());
Log.i("xxx", "onFailure " +errorCode);
}
});
String basePtah = Environment.getExternalStorageDirectory().getPath();
String[] path = new String[]{basePtah+ "/setting.cfg",basePtah+"/Screenshot.png"};
XHttp.getInstance().post(WebApi.LOGIN_URL)
.setParams(USER_NAME, mName.getText().toString())
.setParams(USER_PASSWORD, mAge.getText().toString())
.setParams("tag", Code.TAG_LOGIN)
.setParams("platform", "mobile_phone")
.setOnXHttpCallback(new OnXHttpCallback() {
@Override
public void onSuccess(final Response response) {
JJLogger.logInfo(TAG, "MainActivity.onSuccess :" +
response.toString());
Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(final Exception ex, final String errorCode) {
JJLogger.logInfo(TAG, "MainActivity.onFailure :" + ex.getMessage());
}
});
String basePtah = Environment.getExternalStorageDirectory().getPath();
String path = basePtah+ "/setting.cfg";
XHttp.getInstance()
.post(WebApi.UPLOAD_FILE_URL)//url
.setHeads("platform","mobile_phone")//设置请求头
.uploadFile(path)//文件路径(完整路径)
.setOnXHttpCallback(new OnXHttpCallback() {
@Override
public void onSuccess(final Response response) {
JJLogger.logInfo(TAG,"MainActivity.onSuccess :"+
response.toString());
Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(final Exception ex, final String errorCode) {
JJLogger.logInfo(TAG,"MainActivity.onFailure :"+ex.getMessage());
}
});
final StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 20; i++) {
final int finalI = i;
XHttp.getInstance()
.post(WebApi.LOGIN_URL)
.setParams("account", mName.getText().toString())
.setParams("password", mAge.getText().toString())
.startConcurrenceThreadPool()
.setOnXHttpCallback(new OnXHttpCallback() {
@Override
public void onSuccess(final Response response) {
Log.i("task","MainActivity.onSuccess 任务"+ finalI +"完成:");
stringBuilder.append("并行 任务"+ finalI +"完成:").append("\n");
mTextView1.setText(stringBuilder.toString());
}
@Override
public void onFailure(final Exception ex, final String errorCode) {
JJLogger.logInfo(TAG, "MainActivity.onFailure :" + ex.getMessage());
}
});
}
final StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 20; i++) {
final int finalI = i;
XHttp.getInstance()
.post(WebApi.LOGIN_URL)
.setParams("account", mName.getText().toString())
.setParams("password", mAge.getText().toString())
.startSerialThreadPool()
.setOnXHttpCallback(new OnXHttpCallback() {
@Override
public void onSuccess(final Response response) {
Log.i("task","MainActivity.onSuccess 任务"+ finalI +"完成:");
stringBuilder.append("串行 任务"+ finalI +"完成:").append("\n");
mTextView2.setText(stringBuilder.toString());
}
@Override
public void onFailure(final Exception ex, final String errorCode) {
JJLogger.logInfo(TAG, "MainActivity.onFailure :" + ex.getMessage());
}
});
}
XHttp.getInstance().cancel("bbb");
XHttp.getInstance().cancelAll();