android-async-http主要特征:处理异步Http请求,并通过匿名内部类处理回调结果,Http异步请求均位于非UI线程,不会阻塞UI操作,通过线程池处理并发请求处理文件上传、下载,响应结果自动打包JSON格式.自动处理连接断开时请求重连.使用android-async-http也非常简单,到官网http://loopj.com/android-async-http/下载依赖jar包,导入工程中libs文件夹下并添加到工程路径即可。通过下面的代码来创建一个异步请求
async-http的GET方式使用
private void asyncHttpClientGet() {
//创建一个请求客户端
AsyncHttpClient client = new AsyncHttpClient();
String url = "http://10.150.249.211:8080/TestServer/api.jsp?action=login&machine_id=1&";
//client使用GET方式进行网络访问
client.get(url,new AsyncHttpResponseHandler(){
@Override
public void onSuccess(String content) {//请求成功回调
super.onSuccess(content);
Toast.makeText(MainActivity.this,content,Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Throwable error) {//请求失败回调
super.onFailure(error);
Toast.makeText(MainActivity.this,"请求失败",Toast.LENGTH_SHORT).show();
}
});
}
async-http的POST方式使用
private void asyncHttpClientPost() {
//创建一个请求客户端
AsyncHttpClient client = new AsyncHttpClient();
String url = "http://10.150.249.211:8080/TestServer/api.jsp";
//创建请求参数
RequestParams params = new RequestParams();
params.put("action","login");
params.put("machine_id","2");
//client使用POST方式访问
client.post(url,params,new AsyncHttpResponseHandler(){
@Override
public void onSuccess(String content) {//请求成功回调
super.onSuccess(content);
Toast.makeText(MainActivity.this,content,Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Throwable error) {//请求失败回调
super.onFailure(error);
Toast.makeText(MainActivity.this,"请求失败",Toast.LENGTH_SHORT).show();
}
});
}
自定义基于async-http库的网络访问基类
package com.example.zhaoyun.usingasynchttpclient;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
public class RequestUtils {
public static AsyncHttpClient client = new AsyncHttpClient();
public static void clientGET(String url, NetConnectionCallback callback){
client.get(url, callback);
}
public static void clientPOST(String url, RequestParams params, NetConnectionCallback callback){
client.post(url, params, callback);
}
}
package com.example.zhaoyun.usingasynchttpclient;
import android.util.Log;
import com.loopj.android.http.AsyncHttpResponseHandler;
//自定义网络请求回调类,扩展自AsyncHttpResponseHandler
public abstract class NetConnectionCallback extends AsyncHttpResponseHandler {
@Override
public void onStart() {
Log.i("info","请求开始,呈现进度提示框");
super.onStart();
}
@Override
public void onSuccess(String content) {
Log.i("info","请求成功,隐藏进度提示框" + content);
MyOnSuccess(content);
super.onSuccess(content);
}
@Override
public void onFailure(Throwable error) {
Log.i("info","请求失败,隐藏进度提示框" + error);
MyOnFailure(error);
super.onFailure(error);
}
//抽象方法用于向主类传递请求回调结果
public abstract void MyOnSuccess(String content);
public abstract void MyOnFailure(Throwable error);
}
在主类中使用自定义的网络访问类
package com.example.zhaoyun.usingasynchttpclient;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnRead = (Button) findViewById(R.id.btnRead);
btnRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
asyncHttpClientPost();
}
});
}
private void asyncHttpClientPost() {
String url = "http://10.150.249.211:8080/TestServer/api.jsp";
//创建请求参数
RequestParams params = new RequestParams();
params.put("action","login");
params.put("machine_id","2");
//client使用POST方式访问
RequestUtils.clientPOST(url, params, new NetConnectionCallback() {
@Override
public void MyOnSuccess(String content) {
Toast.makeText(MainActivity.this, content, Toast.LENGTH_SHORT).show();
}
@Override
public void MyOnFailure(Throwable error) {
Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
}
});
}
private void asyncHttpClientGet() {
String url = "http://10.150.249.211:8080/TestServer/api.jsp?action=login&machine_id=1&";
//client使用GET方式进行网络访问
RequestUtils.clientGET(url, new NetConnectionCallback() {
@Override
public void MyOnSuccess(String content) {
Toast.makeText(MainActivity.this, content, Toast.LENGTH_SHORT).show();
}
@Override
public void MyOnFailure(Throwable error) {
Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
}
});
}
private Button btnRead;
}
测试结果