能成功同步
1)AsyncHttpClient:核心类,使用HttpClient执行网络请求,提供get,put,post,delete,head等请求方法
只需以url及RequestParams调用相应的方法即可,必须提供ResponseHandlerInterface的实现类,一般为
AsyncHttpResponseHandler的子类,AsyncHttpClient内部有一个线程池,当使用AsyncHttpClient执行
网络请求时,最终都会调用sendRequest方法,在这个方法内部将请求参数封装成AsyncHttpRequest交由内
部的线程池执行
2)SyncHttpClient:继承自AsyncHttpClient,同步执行网络请求,AsyncHttpClient把请求封装成AsyncHttpRequest
后直接调用它的run方法
3)AsyncHttpRequest:继承自Runnabler,被submit至线程池执行网络请求并发送start,success等消息
4)RequestParams:请求参数,可以添加字符串参数,并可添加File,InputStream上传文件
5)AsyncHttpResponseHandler:接收请求结果,一般重写onSuccess及onFailure接收请求成功或失败的消息
还有onStart,onFinish等消息
6)TextHttpResponseHandler:继承自AsyncHttpResponseHandler,将请求结果由byte数组转成了String
7)JsonHttpResponseHandler:继承自TextHttpResponseHandler,将请求结果由String转换成JSONObject
或JSONArray
8)BaseJsonHttpResponseHandler:继承自TextHttpResponseHandler,是一个泛型类,可以直接原始解析
在Module:app里添加
dependencies {
compile 'com.loopj.android:android-async-http:1.4.9'
}
json测试数据:
{"people": [{"name":"zhangbing"},{"name": "linbing"},{"name": "lingbing"}]}
public class MainActivity extends AppCompatActivity {
private Button btGet;
private Button btPost;
private Button btDownload;
private Button btUpLoad;
private Button btJson;
private Button btPicture;
private ImageView iv;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btGet = (Button)findViewById(R.id.btGet);
btPost = (Button)findViewById(R.id.btPost);
btDownload = (Button)findViewById(R.id.btDownload);
btUpLoad = (Button)findViewById(R.id.btUpload);
btJson = (Button)findViewById(R.id.btJson);
btPicture = (Button)findViewById(R.id.btPicture);
iv = (ImageView)findViewById(R.id.imageView);
tv = (TextView)findViewById(R.id.textView);
btGet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AsyncHttpClient client= new AsyncHttpClient();
client.get("https://www.baidu.com", new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
tv.setText(responseString);
Log.i("tag",responseString);
}
});
}
});
btPost.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = "http://apicloud.mob.com/v1/mobile/address/query";
AsyncHttpClient http = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.add("key","1922d51a5257d");
params.add("phone","17781046785");
http.post(url, params, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
tv.setText(responseString);
}
});
}
});
//下载文件到内置SD卡
btDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = "http://192.168.42.144:8080/CompassDemo.zip";
//下载之后存放的路径Environment.getExternalStorageDirectory()
File file = new File(Environment.getExternalStorageDirectory(),url.substring(url.lastIndexOf("/")+1));
AsyncHttpClient http = new AsyncHttpClient();
http.get(url, new FileAsyncHttpResponseHandler(file) {
@Override
public void onStart() {
super.onStart();
tv.setText("开始下载...");
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, File file) {
tv.setText("下载失败...");
}
@Override
public void onSuccess(int statusCode, Header[] headers, File file) {
if(statusCode == 200){
tv.setText("下载文件成功:下载路径:"+file.getPath());
}
}
});
}
});
//上传文件
btUpLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url="http://192.168.42.144:8080/";
AsyncHttpClient http = new AsyncHttpClient();
RequestParams params = new RequestParams();
File file = new File("/storage/emulated/0/wifi");
try{
params.put("file",file);
http.post(url,params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
if(statusCode==200){
tv.setText("上传成功");
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
tv.setText("上传失败");
}
});
}catch (FileNotFoundException e){
e.printStackTrace();
}
}
});
btJson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = "http://192.168.42.144:8080/gson";
AsyncHttpClient http = new AsyncHttpClient();
http.get(url, new JsonHttpResponseHandler(){
//onSuccess方法里response参数类型应该根据返回值类型设定
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
if(statusCode == 200){
//遍历json数组
try{
JSONArray array = response.getJSONArray("people");
StringBuilder stringBuilder = new StringBuilder();
for(int i=0; i< array.length(); i++){
stringBuilder.append(array.getJSONObject(i).getString("name")+"\n");
}
tv.setText(stringBuilder.toString());
}catch (JSONException e){
e.printStackTrace();
}
}
}
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
super.onFailure(statusCode, headers, responseString, throwable);
tv.setText("Json下载失败");
}
});
}
});
btPicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = "http://192.168.42.144:8080/flower.jpeg";
AsyncHttpClient client = new AsyncHttpClient();
client.get(url, new BinaryHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] binaryData) {
Bitmap bmp = BitmapFactory.decodeByteArray(binaryData,0, binaryData.length);
iv.setImageBitmap(bmp);
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] binaryData, Throwable error) {
}
});
}
});
}
}