首先需要:android-async-http-1.4.3.jar
HttpUtil.java
package com.queueup.network;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.BinaryHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
public class HttpUtil {
private static AsyncHttpClient client = new AsyncHttpClient(); // 实例话对象
static {
client.setTimeout(5000); // 设置链接超时,如果不设置,默认为5s
}
public static AsyncHttpClient getClient() {
return client;
}
/************************ GET ************************************************************************/
public static void get(String urlString, AsyncHttpResponseHandler res) // 用一个完整url获取一个string对象
{
client.get(urlString, res);
}
public static void get(String urlString, RequestParams params,
AsyncHttpResponseHandler res) // url里面带参数
{
client.get(urlString, params, res);
}
public static void get(String urlString, JsonHttpResponseHandler res) // 不带参数,获取json对象或者数组
{
client.get(urlString, res);
}
public static void get(String urlString, RequestParams params,
JsonHttpResponseHandler res) // 带参数,获取json对象或者数组
{
client.get(urlString, params, res);
}
public static void get(String uString, BinaryHttpResponseHandler bHandler) // 下载数据使用,会返回byte数据
{
client.get(uString, bHandler);
}
/********************** POST **************************************************************************/
public static void post(String urlString, RequestParams params,
AsyncHttpResponseHandler res) // url里面带参数
{
client.post(urlString, params, res);
}
public static void post(String urlString, RequestParams params,
JsonHttpResponseHandler res) // 带参数,获取json对象或者数组
{
client.post(urlString, params, res);
}
}
RequestHttp.java
package com.queueup.network;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
public class RequestHttp {
private final String TAG = "HTTP";
/**
* 开始请求
*/
public static final int REQUESTDATA_START = 001;
/**
* 请求连接成功
*/
public static final int REQUESTDATA_SUCCESS_OK = 002;
/**
* 请求连接失败
*/
public static final int REQUESTDATA_SUCCESS_NO = 003;
/**
* 请求失败
*/
public static final int REQUESTDATA_FAILURE = 004;
/**
* 请求完成
*/
public static final int REQUESTDATA_FINISH = 005;
/**
* Get请求
*/
public static final int GET = 006;
/**
* Post请求
*/
public static final int POST = 007;
private Handler mHandler;
private RequestParams mParams;//请求参数
private String request_url;//请求url地址
public RequestHttp(Handler handler, String url) {
this.mHandler = handler;
this.request_url = url;
requestGetData();
}
public RequestHttp(Handler handler, String url, RequestParams params,
int request_type) {
this.mHandler = handler;
this.request_url = url;
this.mParams = params;
switch (request_type) {
case GET:
requestGetParamsData();
break;
case POST:
requestPostParamsData();
break;
default:
break;
}
}
/**
* 普通Get请求,不加参数
*/
private void requestGetData() {
HttpUtil.get(request_url, new AsyncHttpResponseHandler() {
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.e(TAG, "onStart()开始请求");
Log.w(TAG, request_url);
mHandler.sendEmptyMessage(REQUESTDATA_START);
}
@Override
public void onSuccess(int arg0, String arg1) {
// TODO Auto-generated method stub
super.onSuccess(arg0, arg1);
Message message = Message.obtain();
if (arg0 == 200) {
Log.e(TAG, "onSuccess()请求连接成功" + " 请求码:" + arg0);
message.what = REQUESTDATA_SUCCESS_OK;
message.obj = arg1;
mHandler.sendMessage(message);
} else {
Log.e(TAG, "onSuccess()请求连接失败" + " 请求码:" + arg0);
mHandler.sendEmptyMessage(REQUESTDATA_SUCCESS_NO);
}
}
@Override
public void onFailure(Throwable arg0, String arg1) {
// TODO Auto-generated method stub
super.onFailure(arg0, arg1);
Log.e(TAG, "onFailure()请求失败");
mHandler.sendEmptyMessage(REQUESTDATA_FAILURE);
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
super.onFinish();
Log.e(TAG, "onFinish()请求完成");
mHandler.sendEmptyMessage(REQUESTDATA_FINISH);
}
});
}
/**
* Get请求,加参数
*/
private void requestGetParamsData() {
HttpUtil.get(request_url, mParams, new AsyncHttpResponseHandler() {
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.e(TAG, "onStart()开始请求");
Log.w(TAG, request_url + mParams.toString().trim());
mHandler.sendEmptyMessage(REQUESTDATA_START);
}
@Override
public void onSuccess(int arg0, String arg1) {
// TODO Auto-generated method stub
super.onSuccess(arg0, arg1);
Log.e(TAG, "onSuccess()请求成功" + " 请求码:" + arg0);
Message message = Message.obtain();
if (arg0 == 200) {
message.what = REQUESTDATA_SUCCESS_OK;
message.obj = arg1;
mHandler.sendMessage(message);
} else {
mHandler.sendEmptyMessage(REQUESTDATA_SUCCESS_NO);
}
}
@Override
public void onFailure(Throwable arg0, String arg1) {
// TODO Auto-generated method stub
super.onFailure(arg0, arg1);
Log.e(TAG, "onFailure()请求失败");
mHandler.sendEmptyMessage(REQUESTDATA_FAILURE);
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
super.onFinish();
Log.e(TAG, "onFinish()请求完成");
mHandler.sendEmptyMessage(REQUESTDATA_FINISH);
}
});
}
/*********************************************************************************************/
/**
* Post请求,提交表单
*/
private void requestPostParamsData() {
HttpUtil.get(request_url, mParams, new AsyncHttpResponseHandler() {
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.e(TAG, "onStart()开始请求");
Log.w(TAG, request_url + mParams.toString().trim());
mHandler.sendEmptyMessage(REQUESTDATA_START);
}
@Override
public void onSuccess(int arg0, String arg1) {
// TODO Auto-generated method stub
super.onSuccess(arg0, arg1);
Log.e(TAG, "onSuccess()请求成功" + " 请求码:" + arg0);
Message message = Message.obtain();
if (arg0 == 200) {
message.what = REQUESTDATA_SUCCESS_OK;
message.obj = arg1;
mHandler.sendMessage(message);
} else {
mHandler.sendEmptyMessage(REQUESTDATA_SUCCESS_NO);
}
}
@Override
public void onFailure(Throwable arg0, String arg1) {
// TODO Auto-generated method stub
super.onFailure(arg0, arg1);
Log.e(TAG, "onFailure()请求失败");
mHandler.sendEmptyMessage(REQUESTDATA_FAILURE);
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
super.onFinish();
Log.e(TAG, "onFinish()请求完成");
mHandler.sendEmptyMessage(REQUESTDATA_FINISH);
}
});
}
}
把以上两个类,和jar包拷贝到工程里面,下面贴用法:
package com.example.requestdata;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.loopj.android.http.RequestParams;
public class MainActivity extends Activity {
private Button mButton;
private TextView mTextView;
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case RequestData.REQUESTDATA_START:
break;
case RequestData.REQUESTDATA_SUCCESS_OK:
String content = (String) msg.obj;
System.out.println("content" + content);
mTextView.setText(content);
break;
case RequestData.REQUESTDATA_SUCCESS_NO:
break;
case RequestData.REQUESTDATA_FAILURE:
break;
case RequestData.REQUESTDATA_FINISH:
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) this.findViewById(R.id.button1);
mTextView = (TextView) this.findViewById(R.id.textView2);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 不带参数
// new RequestData(
// mHandler,
// "http://192.168.1.221:8080/QueueServer/resources/Company/getNearCompany/?MaxResults=10&CompanyType=restaurant&MemberId=111111&MemberAddressLongitude=116.619869&MemberAddressLatitude=39.935686&FirstResult=0");
// 带参数
String urlString = "http://192.168.1.221:8080/QueueServer/resources/Company/getNearCompany/?";
RequestParams mParams = new RequestParams();
mParams.put("MemberAddressLatitude", "39.935686");
mParams.put("MemberAddressLongitude", "116.619869");
mParams.put("FirstResult", String.valueOf(0));
mParams.put("MaxResults", String.valueOf(10));
mParams.put("MemberId", "111111");
mParams.put("CompanyType", "restaurant");
// 需要传进去一个Handler
new RequestData(mHandler, urlString, mParams, RequestData.GET);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
menu.getItem(0).setOnMenuItemClickListener(
new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "清除数据",
Toast.LENGTH_LONG).show();
mTextView.setText(null);
return false;
}
});
return true;
}
}