HttplClientUtils.java
package com.mps.android.commons;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.R.integer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
public class HttpClientUtils implements Runnable {
public static final String TAG = "mps";
//用于区分Handler里的类型
public static final int DO_START = 0 ;
public static final int DO_ERROR = 1 ;
public static final int DO_SUCCEED = 2;
//method类型
public static final int GET = 0;
public static final int POST = 1;
/**
* 请求地址
*/
private String url ;
/**
* 请求类型
*/
private int method;
private Handler handler;
/**
*请求参数
*/
private List<NameValuePair> data;
private HttpClient httpClient;
/**
* 无参构造方法
* 自己new一个handler
*/
public HttpClientUtils(){
this(new Handler());
}
/**
* 构造方法
* @param _handler
*/
public HttpClientUtils(Handler _handler){
handler = _handler;
}
/**
* 创建一个线程去请求服务器数据
* @param method 请求类型
* @param url 请求地址
* @param data 请求参数
*/
public void createRunnable(int method,String url,List<NameValuePair> data){
Log.d(TAG, "method:"+method+" ,url:"+url+" ,data:"+data);
this.method = method;
this.url = url;
this.data = data;
HttpClientManager.getInstance().pushRunnale(this);
}
public void get(String url) {
createRunnable(GET, url, null);
}
public void post(String url, List<NameValuePair> data) {
createRunnable(POST, url, data);
}
@Override
public void run() {
//开始执行线程
handler.sendMessage(Message.obtain(handler, DO_START));
httpClient = new DefaultHttpClient();
//设置超时时间
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 5000);
try {
HttpResponse response = null ;
switch (method) {
case GET:
HttpGet httpGet = new HttpGet(url);
response = httpClient.execute(httpGet);
break;
case POST:
HttpPost httpPost = new HttpPost(url);
UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(data,HTTP.UTF_8);
httpPost.setEntity(postEntity);
response = httpClient.execute(httpPost);
break;
}
processEntity(response.getEntity());
} catch (Exception e) {
handler.sendMessage(Message.obtain(handler,DO_ERROR,e));
}
}
private void processEntity(HttpEntity entity) throws Exception{
byte[] data = EntityUtils.toByteArray(entity);
String result = new String(data);
handler.sendMessage(Message.obtain(handler,DO_SUCCEED,result));
}
}
HttpClientManager.java
package com.mps.android.commons;
import java.util.ArrayList;
import java.util.List;
public class HttpClientManager {
public static final int MAX_CLINETS = 5;
/**
* 正在跑的线程列表
*/
private List<Runnable> active = new ArrayList<Runnable>();
/**
* 列队线程列表
*/
private List<Runnable> queue = new ArrayList<Runnable>();
private static HttpClientManager instance;
public static HttpClientManager getInstance(){
if(instance == null){
instance = new HttpClientManager();
}
return instance;
}
/**
* 添加一个线程到列队中
* @param runnable
*/
public void pushRunnale(Runnable runnable){
queue.add(runnable);
//如果正在执行的线程数不到最大数,则继续执行
if(active.size() < MAX_CLINETS){
startNext();
}
}
/**
* 执行列队中下一个线程
*/
public void startNext(){
//列队不为空
if(!queue.isEmpty()){
Runnable next = queue.get(0);
queue.remove(0);
active.add(next);
Thread thread = new Thread(next);
thread.run();
}
}
/**
* 执行完成,将Runnable从列表中删除,并执行列队中下一个
* @param runnable
*/
public void doRunnableComplete(Runnable runnable){
active.remove(runnable);
startNext();
}
}
HttpClientHandler.java
package com.mps.android.commons;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
public class HttpClientHandler extends Handler {
private Context context ;
/**
* 对话框
*/
private ProgressDialog progressDialog;
public HttpClientHandler(Context context) {
this.context = context;
}
/**
* 进程开始执行
*/
protected void startClient() {
progressDialog = ProgressDialog.show(context, "Please Wait...", "processing...", true);
Log.d(HttpClientUtils.TAG, "Http connection Start...");
}
/**
* 执行成功
* @param json 返回的json
*/
protected void succeed(String json) {
if(progressDialog!=null && progressDialog.isShowing()){
progressDialog.dismiss();
}
}
public void handleMessage(Message msg) {
//判断what类型做出相应操作
switch (msg.what) {
case HttpClientUtils.DO_START:
startClient();
break;
case HttpClientUtils.DO_SUCCEED:
String result = (String)msg.obj;
Log.d(HttpClientUtils.TAG, "Http return :"+result);
succeed(result);
break;
case HttpClientUtils.DO_ERROR:
if(progressDialog!=null && progressDialog.isShowing()){
progressDialog.dismiss();
}
Exception e = (Exception)msg.obj;
e.printStackTrace();
Log.e(HttpClientUtils.TAG, "Error:"+e.getMessage());
break;
}
}
}
调用
private HttpClientHandler handler = new HttpClientHandler(MainActivity.this){
@Override
protected void succeed(String json) {
// 可重写HttpClientHandler里面的方法
super.succeed(json);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
HttpClientUtils client = new HttpClientUtils(handler);
List<NameValuePair> data = new ArrayList<NameValuePair>();
data.add(new BasicNameValuePair("method", "json"));
client.post("http://10.3.188.20:9080/cusi/s.do", data);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Error-->" + e.toString());
}
}