1 package alex.reader.ebook.bam; 2 import java.io.IOException; 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.Iterator; 6 import java.util.List; 7 import java.util.Map; 8 import org.apache.http.HttpResponse; 9 import org.apache.http.NameValuePair; 10 import org.apache.http.client.ClientProtocolException; 11 import org.apache.http.client.HttpClient; 12 import org.apache.http.client.entity.UrlEncodedFormEntity; 13 import org.apache.http.client.methods.HttpGet; 14 import org.apache.http.client.methods.HttpPost; 15 import org.apache.http.client.params.HttpClientParams; 16 import org.apache.http.impl.client.DefaultHttpClient; 17 import org.apache.http.message.BasicNameValuePair; 18 import org.apache.http.params.BasicHttpParams; 19 import org.apache.http.params.HttpConnectionParams; 20 import org.apache.http.params.HttpParams; 21 import org.apache.http.params.HttpProtocolParams; 22 import org.apache.http.protocol.HTTP; 23 import org.apache.http.util.EntityUtils; 24 import android.app.Activity; 25 import android.os.Bundle; 26 import android.util.Log; 27 import android.widget.EditText; 28 public class SimpleClient extends Activity { 29 private HttpParams httpParams; 30 private HttpClient httpClient; 31 @Override 32 public void onCreate(Bundle savedInstanceState) { 33 super.onCreate(savedInstanceState); 34 setContentView(R.layout.simple_client); 35 EditText editText = (EditText) this.findViewById(R.id.EditText01); 36 List<NameValuePair> params = new ArrayList<NameValuePair>(); 37 params.add(new BasicNameValuePair("email", "firewings.r@gmail.com")); 38 params.add(new BasicNameValuePair("password", "954619")); 39 params.add(new BasicNameValuePair("remember", "1")); 40 params.add(new BasicNameValuePair("from", "kx")); 41 params.add(new BasicNameValuePair("login", "登 录")); 42 params.add(new BasicNameValuePair("refcode", "")); 43 params.add(new BasicNameValuePair("refuid", "0")); 44 Map params2 = new HashMap(); 45 params2.put("hl", "zh-CN"); 46 params2.put("source", "hp"); 47 params2.put("q", "haha"); 48 params2.put("aq", "f"); 49 params2.put("aqi", "g10"); 50 params2.put("aql", ""); 51 params2.put("oq", ""); 52 String url2 = "http://www.google.cn/search"; 53 String url = "http://wap.kaixin001.com/home/"; 54 getHttpClient(); 55 editText.setText(doPost(url, params)); 56 // editText.setText(doGet(url2, params2)); 57 } 58 public String doGet(String url, Map params) { 59 /* 建立HTTPGet对象 */ 60 String paramStr = ""; 61 Iterator iter = params.entrySet().iterator(); 62 while (iter.hasNext()) { 63 Map.Entry entry = (Map.Entry) iter.next(); 64 Object key = entry.getKey(); 65 Object val = entry.getValue(); 66 paramStr += paramStr = "&" + key + "=" + val; 67 } 68 if (!paramStr.equals("")) { 69 paramStr = paramStr.replaceFirst("&", "?"); 70 url += paramStr; 71 } 72 HttpGet httpRequest = new HttpGet(url); 73 String strResult = "doGetError"; 74 try { 75 /* 发送请求并等待响应 */ 76 HttpResponse httpResponse = httpClient.execute(httpRequest); 77 /* 若状态码为200 ok */ 78 if (httpResponse.getStatusLine().getStatusCode() == 200) { 79 /* 读返回数据 */ 80 strResult = EntityUtils.toString(httpResponse.getEntity()); 81 } else { 82 strResult = "Error Response: " 83 + httpResponse.getStatusLine().toString(); 84 } 85 } catch (ClientProtocolException e) { 86 strResult = e.getMessage().toString(); 87 e.printStackTrace(); 88 } catch (IOException e) { 89 strResult = e.getMessage().toString(); 90 e.printStackTrace(); 91 } catch (Exception e) { 92 strResult = e.getMessage().toString(); 93 e.printStackTrace(); 94 } 95 Log.v("strResult", strResult); 96 return strResult; 97 } 98 public String doPost(String url, List<NameValuePair> params) { 99 /* 建立HTTPPost对象 */ 100 HttpPost httpRequest = new HttpPost(url); 101 String strResult = "doPostError"; 102 try { 103 /* 添加请求参数到请求对象 */ 104 httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); 105 /* 发送请求并等待响应 */ 106 HttpResponse httpResponse = httpClient.execute(httpRequest); 107 /* 若状态码为200 ok */ 108 if (httpResponse.getStatusLine().getStatusCode() == 200) { 109 /* 读返回数据 */ 110 strResult = EntityUtils.toString(httpResponse.getEntity()); 111 } else { 112 strResult = "Error Response: " 113 + httpResponse.getStatusLine().toString(); 114 } 115 } catch (ClientProtocolException e) { 116 strResult = e.getMessage().toString(); 117 e.printStackTrace(); 118 } catch (IOException e) { 119 strResult = e.getMessage().toString(); 120 e.printStackTrace(); 121 } catch (Exception e) { 122 strResult = e.getMessage().toString(); 123 e.printStackTrace(); 124 } 125 Log.v("strResult", strResult); 126 return strResult; 127 } 128 public HttpClient getHttpClient() { 129 // 创建 HttpParams 以用来设置 HTTP 参数(这一部分不是必需的) 130 this.httpParams = new BasicHttpParams(); 131 // 设置连接超时和 Socket 超时,以及 Socket 缓存大小 132 HttpConnectionParams.setConnectionTimeout(httpParams, 20 * 1000); 133 HttpConnectionParams.setSoTimeout(httpParams, 20 * 1000); 134 HttpConnectionParams.setSocketBufferSize(httpParams, 8192); 135 // 设置重定向,缺省为 true 136 HttpClientParams.setRedirecting(httpParams, true); 137 // 设置 user agent 138 String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2) Gecko/20100115 Firefox/3.6"; 139 HttpProtocolParams.setUserAgent(httpParams, userAgent); 140 // 创建一个 HttpClient 实例 141 // 注意 HttpClient httpClient = new HttpClient(); 是Commons HttpClient 142 // 中的用法,在 Android 1.5 中我们需要使用 Apache 的缺省实现 DefaultHttpClient 143 httpClient = new DefaultHttpClient(httpParams); 144 return httpClient; 145 } 146 }
转自:http://blog.csdn.net/litton_van/article/details/6544086