下载jar包
android-async-http-1.4.9.jar
httpclient-4.3.6.jar
HTTP请求
String url = "";
get
RequestParams requestParams = new RequestParams();
requestParams.put("username", "abc");
requestParams.put("password", "123");
client.get(url, requestParams, jsonHttpResponseHandler );
post
RequestParams requestParams = new RequestParams();
requestParams.put("username", "abc");
requestParams.put("password", "123");
client.post(url, requestParams, jsonHttpResponseHandler);
上传文件
String path1 = "";
String path2 = "";
RequestParams requestParams = new RequestParams();
File file1 = new File(path1);
File file2 = new File(path2);
try {
requestParams.put("file1", file1);
requestParams.put("file2", file2);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
file1 = null;
file2 = null;
client.post(url, requestParams, jsonHttpResponseHandler);
下载文件
RequestParams requestParams = new RequestParams();
requestParams.put("version", "1.1.0");
client.post(url, requestParams, binaryHttpResponseHandler);
BinaryHttpResponseHandler类
BinaryHttpResponseHandler binaryHttpResponseHandler = new BinaryHttpResponseHandler() {
@Override
public void onStart() {
}
@Override
public void onProgress(long bytesWritten, long totalSize) {
}
@Override
public void onSuccess(int i, Header[] headers, byte[] bytes) {
}
@Override
public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
}
};
JsonHttpResponseHandler类
JsonHttpResponseHandler jsonHttpResponseHandler = new JsonHttpResponseHandler() {
@Override
public void onStart() {
LogUtil.e_long("onStart", "onStart");
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
LogUtil.e_long("onSuccess", "JSONObject: "+ response.toString());
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
LogUtil.e_long("onSuccess", "JSONArray: "+ response.toString());
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
LogUtil.e_long("onSuccess", "responseString: "+ response.toString());
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
LogUtil.e_long("onFailure", "Throwable: " + throwable.getMessage() + "\nJSONObject: " + errorResponse.toString());
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
LogUtil.e_long("onFailure", "Throwable: " + throwable.getMessage() + "\nJSONArray: " + errorResponse.toString());
}
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
LogUtil.e_long("onFailure", "Throwable: " + throwable.getMessage() + "\nresponseString: " + responseString);
}
@Override
public void onFinish() {
LogUtil.e_long("onFinish", "onFinish");
}
};
当有返回数据但是不是json格式时,会调用
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
LogUtil.e_long("onFailure", "responseString");
}
方法。
由于该回调太过复杂,我用不成熟的手法简单封装了一下(json解析库为fastjson):
public abstract class MyJsonResponseHandler extends JsonHttpResponseHandler {
private Class<?> t;
public MyJsonResponseHandler(Class<?> t) {
this.t = t;
}
@Override
public void onStart() {
LogUtil.e_long("onStart", "onStart");
}
@Override
public final void onSuccess(int statusCode, Header[] headers, JSONObject response) {
LogUtil.e_long("onSuccess", "JSONObject: "+ response.toString());
dealResponse(response.toString());
}
@Override
public final void onSuccess(int statusCode, Header[] headers, JSONArray response) {
LogUtil.e_long("onSuccess", "JSONArray: "+ response.toString());
dealResponse(response.toString());
}
@Override
public final void onSuccess(int statusCode, Header[] headers, String responseString) {
LogUtil.e_long("onSuccess", "responseString: "+ responseString);
dealResponse(responseString);
}
@Override
public final void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
LogUtil.e_long("onFailure", "Throwable: " + throwable.getMessage() + "\nJSONObject: " + errorResponse.toString());
onFailure(throwable);
}
@Override
public final void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
LogUtil.e_long("onFailure", "Throwable: " + throwable.getMessage() + "\nJSONArray: " + errorResponse.toString());
onFailure(throwable);
}
@Override
public final void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
LogUtil.e_long("onFailure", "Throwable: " + throwable.getMessage() + "\nresponseString: " + responseString);
onFailure(throwable);
}
@Override
public void onFinish() {
LogUtil.e_long("onFinish", "onFinish");
}
private void dealResponse(String response) {
try {
Object o = JSON.parseObject(response, t);
onSuccess(o);
} catch (Exception ex) {
onFailure(ex);
}
}
public abstract void onSuccess(Object object);
public abstract void onFailure(Throwable throwable);
}
使用时可以这样写:
client.get(url, requestParams, new MyJsonResponseHandler(UserInfoResponseBean.class) {
@Override
public void onSuccess(Object object) {
UserInfoResponseBean responseBean = (UserInfoResponseBean) object;
if (responseBean == null) {
Toast.makeText(MainActivity.this, "获取数据失败", Toast.LENGTH_SHORT).show();
return;
}
if ("1".equals(responseBean.getStat())) {
UserInfoBean userInfoBean = responseBean.getData();
// TODO: do something else...
} else {
if (TextUtils.isEmpty(responseBean.getMsg())) {
Toast.makeText(MainActivity.this, "获取数据失败", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, responseBean.getMsg(), Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onFailure(Throwable throwable) {
}
});
bean类为:
public class UserInfoResponseBean {
private int stat;
private String msg;
private UserInfoBean data;
//setter and getter...
}
通常情况下,返回的json格式包含stat(也有叫code),msg和data三个字段,并和服务器约定stat==1时为正确返回,0为请求结果失败。
所以为了节省代码(省去了UserInfoResponseBean)可以这样写
private void dealResponse(String response) {
try {
com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(response);
if (jsonObject.containsKey("stat") && jsonObject.containsKey("msg") && jsonObject.containsKey("data")) {
String stat = jsonObject.getString("stat");
String msg = jsonObject.getString("msg");
String data = jsonObject.getString("data");
Object o = JSON.parseObject(data, t);
onSuccess("1".equals(stat), msg, o);
} else {
onFailure(new Exception("json does not contain keys called stat msg or data"));
}
} catch (Exception ex) {
onFailure(ex);
}
}
使用时:
client.get(url, requestParams, new MyJsonResponseHandler(UserInfoBean.class) {
@Override
public void onSuccess(boolean isSuccess, String msg, Object object) {
if (isSuccess) {
UserInfoBean userInfoBean = (UserInfoBean) object;
// TODO: do something else ...
} else {
if (TextUtils.isEmpty(msg)) {
Toast.makeText(MainActivity.this, "获取数据失败", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onFailure(Throwable throwable) {
}
});
}
注意
AsyncHttp和xutils回调顺序不一样。
AsyncHttp:
onStart()
onFinish()
onSuccess()/onFailure()
xutils:
onStart()
onSuccess()/onFailure()
onFinish()
网友评论