您的位置:首页 > 理论基础 > 计算机网络

android 封装Http请求的帮助类

2016-04-25 00:04 585 查看
整理好的帮助类,注意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();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: