android 封装Http请求的帮助类

整理好的帮助类,注意Post请求使用编码我用的是utf-8


/** 

 * This class is used for : http request helper
 *  
 * @author limengxiao 
 * @version  
 *     1.0 ${2016_4_24} ${23:58} 
 */  
package com.example.lmxhttpclient;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
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.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;


import android.content.Context;
import android.util.Log;


public abstract class lmxHttpClient {


public Context context;
public String url;
public int timeOut;
public HashMap<String, String> param_map;


public lmxHttpClient(String url, int timeOut,
HashMap<String, String> param_map) {
super();
this.url = url;
this.timeOut = timeOut;
this.param_map = param_map;
}


public abstract void onRequestSuccess(String json);


public abstract void onRequestFailure();


public void doGet() {
final int get_timeOut = timeOut;
final HashMap<String, String> get_paramMap = param_map;
final Context get_context = context;
new Thread() {
public void run() {
try {
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, timeOut);
String get_url = getFullUrl(url, get_paramMap);
Log.i("res", "get请求的完整url:-->" + get_url);
HttpGet get = new HttpGet(get_url);
HttpResponse resp = client.execute(get);
HttpEntity entity = resp.getEntity();
String json = EntityUtils.toString(entity);
onRequestSuccess(json);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
onRequestFailure();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
onRequestFailure();
}
}


}.start();


}


// 为get请求 拼接url
private String getFullUrl(String url, HashMap<String, String> get_paramMap) {
// TODO Auto-generated method stub
StringBuffer sb = new StringBuffer();
if (get_paramMap.size() > 0) {
// 如果请求有参数部分,则基础url必须以'?'结尾
if (!url.endsWith("?")) {
url += "?";
}
}
sb.append(url);
// 加上参数部分
Set<String> keySet = get_paramMap.keySet();
Iterator<String> it = keySet.iterator();
while (it.hasNext()) {
String key = it.next();
sb.append(key + "=" + get_paramMap.get(key) + "&");
}
// 去除最后一个&
sb.deleteCharAt(sb.length() - 1);
String s = sb.toString();
return s;
};


public void doPost() {
final String post_url=url;
final HashMap<String,String> post_map=param_map;
new Thread(){
public void run() {
HttpClient client=new DefaultHttpClient();
client.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, timeOut);
HttpPost post=new HttpPost(post_url);
// Post请求中实体部分需要由list组成,这里为了方便统一使用map,在此将map转成list
ArrayList<BasicNameValuePair> list=new ArrayList<BasicNameValuePair>();
Set<String> keySet=post_map.keySet();
Iterator<String> it=keySet.iterator();
while(it.hasNext()){
String key=it.next();
String value=post_map.get(key);
list.add(new BasicNameValuePair(key,value));
}
try {
// ************如果POST请求失败 请确认是否使用的是utf-8编码 这里默认utf-8*******************
HttpEntity entity=new UrlEncodedFormEntity(list,"utf-8");
post.setEntity(entity);
post.setHeader("Content-Type","application/x-www-form-urlencoded");
HttpResponse resp=client.execute(post);
entity=resp.getEntity();
String json=EntityUtils.toString(entity);
onRequestSuccess(json);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
onRequestFailure();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
onRequestFailure();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
onRequestFailure();
}
};
}.start();
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值