1、加载异步请求包文件

2、java代码

package com.example.asynchttp;

import org.apache.http.Header;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams; import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Toast; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void click(View view){
AsyncHttpClient client = new AsyncHttpClient(); String path = "http://192.168.1.100/android/add.php";
client.get(path, new AsyncHttpResponseHandler() { @Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
// TODO Auto-generated method stub
String result = new String(responseBody);
Toast.makeText(MainActivity.this, result, 0).show(); } @Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "失败!", 0).show(); } });
} public void click2(View view){
AsyncHttpClient client = new AsyncHttpClient();
String url = "http://192.168.0.86/android/add.php"; RequestParams params = new RequestParams();
params.put("username", "zhangsan");
params.put("password", "123456");
client.post(url, params, new AsyncHttpResponseHandler(){ @Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
// TODO Auto-generated method stub
String result = new String(responseBody);
Toast.makeText(MainActivity.this, result, 0).show(); } @Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "请求失败!", 0).show(); } });
} }
05-06 04:21