import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;import java.util.List;import java.util.Map;import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;import org.apache.http.HttpStatus;import org.apache.http.NameValuePair;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.client.params.HttpClientParams;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.params.BasicHttpParams;import org.apache.http.params.HttpConnectionParams;import org.apache.http.params.HttpParams;import org.apache.http.params.HttpProtocolParams;import org.apache.http.protocol.HTTP;import org.apache.http.util.EntityUtils;import android.util.Log;public class CustomHttpClient {
private HttpClient httpClient;private String className = getClass().getName().toString();
private String TAG = className;public CustomHttpClient() {
this.httpClient = getHttpClient(10000, 10000); }public CustomHttpClient(int conTimeout, int soTimeout) {
this.httpClient = getHttpClient(conTimeout, soTimeout); }public CustomHttpClient(HttpClient httpClient) {
this.httpClient = httpClient; }static public HttpClient getHttpClient(int conTimeout, int soTimeout) {
HttpParams httpParams; httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, conTimeout); HttpConnectionParams.setSoTimeout(httpParams, soTimeout); HttpConnectionParams.setSocketBufferSize(httpParams, 8192); HttpClientParams.setRedirecting(httpParams, true); String userAgent = "Mozilla/5.0(Linux;U;Android 2.2.1;en-us;Nexus One Build.FRG83)"; HttpProtocolParams.setUserAgent(httpParams, userAgent); return new DefaultHttpClient(httpParams); }@SuppressWarnings("rawtypes")
public String Getx(String prefixurl, Map<Object, String> params) { String paramstr = ""; String url = prefixurl; Iterator<?> iter = params.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); Object key = entry.getKey(); Object val = entry.getValue(); paramstr += "&" + key + "=" + val; } if (!paramstr.equals("")) { paramstr = paramstr.replaceFirst("&", "?"); url += paramstr; } return Get(url); }public String Get(String url) {
HttpGet httpRequest = new HttpGet(url); String strResult = null; try { HttpResponse httpResponse = httpClient.execute(httpRequest); if (httpResponse.getStatusLine().getStatusCode() == 200) { strResult = EntityUtils.toString(httpResponse.getEntity()); } else { Log.v(TAG, "Error Response: " + httpResponse.getStatusLine().toString()); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return strResult; }public String Post(String url, List<NameValuePair> params) {
HttpPost httpRequest = new HttpPost(url); String strResult = null; try { httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); HttpResponse httpResponse = httpClient.execute(httpRequest); if (httpResponse.getStatusLine().getStatusCode() == 200) { strResult = EntityUtils.toString(httpResponse.getEntity()); } else { Log.v(TAG, "Error Response: " + httpResponse.getStatusLine().toString()); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return strResult; }public InputStream Download(String url) {
InputStream in = null; HttpGet httpget = new HttpGet(url); try { HttpResponse resp = httpClient.execute(httpget); if (HttpStatus.SC_OK == resp.getStatusLine().getStatusCode()) { HttpEntity entity = resp.getEntity(); in = entity.getContent(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return in; }}