android-async-http是专门针对Android在Apache的HttpClient基础上构建的异步的callback-based http client。
所有的请求全在UI线程之外发生,而callback发生在创建它的线程中,应用了Android的Handler发送消息机制。你也可
以把AsyncHttpClient应用在Service中或者后台线程中,库代码会自动识别出它所运行的context。
使用android-async-http只需要三步:
1. 创建一个AsyncHttpClient;
2. (可选的)通过RequestParams对象设置请求参数;
3. 调用AsyncHttpClient的某个get方法,传递你需要的(成功和失败时)callback接口实现,一般都是匿名内部类
项目的官方网站:http://loopj.com/android-async-http/
对应的github地址:https://github.com/loopj/android-async-http
需要导入的第三方开源包com.loopj.android.http
1 package com.example.day12_02login; 2 3 import java.net.URLEncoder; 4 5 import org.apache.http.Header; 6 7 import com.loopj.android.http.AsyncHttpClient; 8 import com.loopj.android.http.AsyncHttpResponseHandler; 9 import com.loopj.android.http.RequestParams; 10 11 import android.app.Activity; 12 import android.os.Bundle; 13 import android.os.Handler; 14 import android.os.Message; 15 import android.view.View; 16 import android.widget.EditText; 17 import android.widget.Toast; 18 19 public class MainActivity extends Activity { 20 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.activity_main); 25 } 26 27 Handler hanlder = new Handler(){ 28 @Override 29 public void handleMessage(Message msg) { 30 super.handleMessage(msg); 31 switch (msg.what) { 32 case 1: 33 Toast.makeText(MainActivity.this, (String)msg.obj, 1).show(); 34 break; 35 case 2: 36 Toast.makeText(MainActivity.this, (String)msg.obj, 0).show(); 37 break; 38 default: 39 break; 40 } 41 } 42 }; 43 44 class Myhandle extends AsyncHttpResponseHandler{ 45 //200 进入 46 @Override 47 public void onSuccess(int statusCode, Header[] headers, 48 byte[] responseBody) { 49 //这个里面表示链接成功。 50 //应该在这里个去处理成功之后的事情。 51 System.out 52 .println("MainActivity.login(...).Myhandle.onSuccess()"+statusCode); 53 Toast.makeText(MainActivity.this, new String(responseBody) , 1).show(); 54 } 55 //!200进入 56 @Override 57 public void onFailure(int statusCode, Header[] headers, 58 byte[] responseBody, Throwable error) { 59 } 60 } 61 62 //Async http client 的get请求 63 public void login(View v){ 64 EditText et_username = (EditText) findViewById(R.id.et_uername); 65 EditText et_password = (EditText) findViewById(R.id.et_password); 66 67 final String username= et_username.getText().toString(); 68 final String password= et_password.getText().toString(); 69 //http://localhost/LoginDemo/servlet/Login?username=wangdao&password=123456 70 String path = "http://192.168.3.30:8080/LoginDemo/servlet/Login?username="+URLEncoder.encode(username)+"&password="+password; 71 72 AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); 73 asyncHttpClient.get(path, new Myhandle()); 74 75 } 76 77 //Async http client Post 请求实现 78 public void login2(View v){ 79 80 EditText et_username = (EditText) findViewById(R.id.et_uername); 81 EditText et_password = (EditText) findViewById(R.id.et_password); 82 83 final String username= et_username.getText().toString(); 84 final String password= et_password.getText().toString(); 85 //http://localhost/LoginDemo/servlet/Login 86 //发请求的线程 87 String path = "http://192.168.3.30:8080/LoginDemo/servlet/Login"; 88 AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); 89 90 RequestParams rp= new RequestParams(); 91 rp.put("username", username); 92 rp.put("password", password); 93 94 asyncHttpClient.post(path, rp, new Myhandle()); 95 } 96 97 }
activity_main.xml和 MyEclipse服务器端代码与 012_01HttpClient用户登录 相同。