android-asyne-http
android-asyne-http是基于Apache HttpClient库之上的一个异步网络请求处理库,网络处理均基于Android的非UI线程,通过回调方法处理请求结果。
其主要特征如下:处理异步Http请求,并通过匿名内部类处理回调结果,Http异步请求均位于非UI线程,不会阻塞UI操作,通过线程池处理并发请求处理文件上传、下载,响应结果自动打包JSON格式,自动处理连接断开时请求重连。
安装android-asyne-http组件
发送一个异步请求
一定加权限
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEM"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
发送一个请求
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.example.vakwapplication.R;
import com.vorlonsoft.android.http.AsyncHttpClient;
import com.vorlonsoft.android.http.AsyncHttpResponseHandler;
import cz.msebera.android.httpclient.Header;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void sendSimpleGetClick(View v){
AsyncHttpClient client = new AsyncHttpClient();
client.get("https://www.baidu.com", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
//200 ok
System.out.println(new String(responseBody));
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
System.out.println("error"+new String(responseBody));
}
});
}
}
发送一个带参数的请求
//发送一个带参数的请求
public void sendParamsClick(View view){
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("username","vince");
params.put("password","123");
client.post(this, "http://10.0.2.2:8080/HttpURL/LoginServlet", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
System.out.println(new String(responseBody));
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
System.out.println(new String(responseBody));
}
});
}
上传文件
public void uploadClick(View view){
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("description","风景图片");
//设置文件
try {
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/a.jpg";
params.put("filename",new File(path),"image/jpeg");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
client.post(this, "http://10.0.2.2:8080/HttpURL/UploadServlet", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
System.out.println(new String(responseBody));
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
System.out.println(new String(responseBody));
}
});
}
下载文件
//下载文件
public void downloadClick(View view){
AsyncHttpClient client = new AsyncHttpClient();
String url = "http://i1.hexunimg.cn/2013-11-06/159411202.jpg";
client.get(url, new FileAsyncHttpResponseHandler(this) {
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, File file) {
//error
}
@Override
public void onSuccess(int statusCode, Header[] headers, File file) {
System.out.println(file.getAbsolutePath());
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/159411202.jpg";
try {
InputStream in = new FileInputStream(file);
OutputStream out = new FileOutputStream(path);
byte[] bytes = new byte[100];
int len = -1;
while ((len=in.read(bytes))!=-1){
out.write(bytes,0,len);
out.flush();
}
out.close();
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("download complete");
}
});
}
JSON请求
//json请求
public void jsonClick(View view){
AsyncHttpClient client = new AsyncHttpClient();
String url = "http://10.0.2.2:8080/HttpURL/LoginServlet";
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("username","vince");
jsonObject.put("password","123");
StringEntity entity = new StringEntity(jsonObject.toString());
client.post(this,url,entity,"application/json",new JsonHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
System.out.println(response.toString());
}
});
} catch (JSONException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}