Android之HttpClient

Android之HttpClientimportjava.io.IOException;importjava.io.InputStream;importjava.util.Iterator;importjava.util.List;importjava.util.Map;importorg.apache.http.Ht

大家好,欢迎来到IT知识分享网。Android之HttpClient

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;
}
}

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/33543.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信