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

网络请求一 : android-async-http

2013-05-30 18:11 288 查看
做客户端开发就要跟上潮流,新技术,好方法要不断更新,这里有个blog 总结了下

http://my.eoe.cn/sisuer/archive/3348.html?f_section=hot

今天介绍下 第9条 android-async-http

          一、异步任务第一步

        开始运用的是android AsyncTask介绍 这篇文章中的技术,

网络请求应该是HttpUtil 工具类,这样的请求可以以get 和post方式进行网络请求,

而参数直接使用url方便在浏览器上查看结果。

 

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.zip.GZIPInputStream;

import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
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.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.HttpEntityWrapper;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicNameValuePair;
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.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

public class HttpUtil {

private static final int TIMEOUT = 10000;
private static final int TIMEOUT_SOCKET = 15000;

public static final String CTWAP = "ctwap";
public static final String CMWAP = "cmwap";
public static final String WAP_3G = "3gwap";
public static final String UNIWAP = "uniwap";
/** @Fields TYPE_NET_WORK_DISABLED : 网络不可用 */
public static final int TYPE_NET_WORK_DISABLED = 0;
/** @Fields TYPE_CM_CU_WAP : 移动联通wap10.0.0.172 */
public static final int TYPE_CM_CU_WAP = 4;
/** @Fields TYPE_CT_WAP : 电信wap 10.0.0.200 */
public static final int TYPE_CT_WAP = 5;
/** @Fields TYPE_OTHER_NET : 电信,移动,联通,wifi 等net网络 */
public static final int TYPE_OTHER_NET = 6;
public static Uri PREFERRED_APN_URI = Uri
.parse("content://telephony/carriers/preferapn");

private static HashMap<String,
HttpClient> sessionMap = new HashMap<String,
HttpClient>();

public HttpUtil() {
}

/*
* 将形如“\u7cfb\u7edf\u7e41\u5fd9\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5\u3002”的字符串解码
* */
public static String decode(String s) {
StringReader s1 = new StringReader(s);
try {
char[] chars = new char[s.length()];
s1.read(chars);
return new String(chars);
} catch (Exception ex) {
}
return null;
}

/**
* get  httpGet
*
* @param url String
* @param name String     连接名称,用以维护session,不需要维持连接时请置null,操作结束后请调用destroy方法
* @param charset String
* @return String
*/
public static String get(String url, String name, String charset) {
if (charset == null)
charset = HTTP.UTF_8;

try {
HttpClient httpclient;

if (name == null) { //不需要维持连接
httpclient = createHttpClient();
} else if (sessionMap.containsKey(name)) {
httpclient = sessionMap.get(name);
} else {
httpclient = createHttpClient();
sessionMap.put(name, httpclient);
}

HttpGet httpget1 = new HttpGet(url);
HttpEntity entity1;
//            synchronized(httpclient){
HttpResponse response1 = httpclient.execute(httpget1);
entity1 = response1.getEntity();
//            }
return EntityUtils.toString(entity1, charset);
} catch (Exception e) {
e.printStackTrace();
}

return null;
}

/**
* post
*
* @param url String
* @param map HashMap     提交表单的键值对
* @param name String     连接名称,用以维护session
* @param charset String
* @return String
*/
public static String post(String url, HashMap<String, String> map,
String name, String charset) {
if (charset == null)
charset = HTTP.UTF_8;

try {
HttpClient httpclient;

if (name == null) { //不需要维持连接
httpclient = createHttpClient();
} else if (sessionMap.containsKey(name)) {
httpclient = sessionMap.get(name);
} else {
httpclient = createHttpClient();
sessionMap.put(name, httpclient);
}

HttpPost httpost = new HttpPost(url);

List<NameValuePair> nvps = new ArrayList<NameValuePair>();

if (map != null) {
Iterator it = map.keySet().iterator();

while (it.hasNext()) {
String key = (String) it.next();
nvps.add(new BasicNameValuePair(key, map.get(key)));
}
}

httpost.setEntity(new UrlEncodedFormEntity(nvps, charset));

//            System.out.println("!!!!!!!!!!!!!!!");
//            long s=System.currentTimeMillis();
String requstStr;
//            synchronized(httpclient){
HttpResponse response = httpclient.execute(httpost);
//            System.out.println((System.currentTimeMillis()-s)/1000);
HttpEntity entity1 = response.getEntity();
requstStr = EntityUtils.toString(entity1, charset);
entity1.consumeContent();
//            }
return requstStr;
} catch (Exception e) {
e.printStackTrace();
}

return null;
}

public static String post(String url, String content,
String name, String charset) {
if (charset == null)
charset = HTTP.UTF_8;

try {
HttpClient httpclient;

if (name == null) { //不需要维持连接
httpclient = createHttpClient();
} else if (sessionMap.containsKey(name)) {
httpclient = sessionMap.get(name);
} else {
httpclient = createHttpClient();
sessionMap.put(name, httpclient);
}

HttpPost httpost = new HttpPost(url);

if (content != null) {
httpost.setEntity(new StringEntity(content,charset));
}
String requstStr;
//            synchronized(httpclient){
HttpResponse response = httpclient.execute(httpost);

HttpEntity entity1 = response.getEntity();
requstStr = EntityUtils.toString(entity1, charset);
entity1.consumeContent();
//            }
return requstStr;
} catch (Exception e) {
e.printStackTrace();
}

return null;
}

public static void destroy(String name) {
HttpClient httpclient = sessionMap.get(name);
httpclient.getConnectionManager().shutdown();
sessionMap.remove(name);
}

/*    private static HttpClient createHttpClient() {

DefaultHttpClient httpclient = new DefaultHttpClient();
System.getProperties().setProperty("httpclient.useragent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; CIBA; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");

try {

httpclient.addRequestInterceptor(new HttpRequestInterceptor() {

public void process(
final HttpRequest request,
final HttpContext context) throws HttpException,
IOException {
if (!request.containsHeader("Accept-Encoding")) {
request.addHeader("Accept-Encoding", "gzip");
}
}

});
httpclient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,30000);
httpclient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT,60000);
httpclient.addResponseInterceptor(new HttpResponseInterceptor() {

public void process(
final HttpResponse response,
final HttpContext context) throws HttpException,
IOException {
HttpEntity entity = response.getEntity();
Header ceheader = entity.getContentEncoding();
if (ceheader != null) {
HeaderElement[] codecs = ceheader.getElements();
for (int i = 0; i < codecs.length; i++) {
if (codecs[i].getName().equalsIgnoreCase("gzip")) {
response.setEntity(
new GzipDecompressingEntity(response.
getEntity()));
return;
}
}
}
}

});

} catch (Exception e) {
e.printStackTrace();
}
return httpclient;

}*/

public static synchronized HttpClient createHttpClient() {
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params,
HTTP.DEFAULT_CONTENT_CHARSET);
HttpProtocolParams.setUseExpectContinue(params, true);
HttpProtocolParams
.setUserAgent(
params,
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; CIBA; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
ConnManagerParams.setTimeout(params, 5000);
HttpConnectionParams.setConnectionTimeout(params, TIMEOUT);
HttpConnectionParams.setSoTimeout(params, TIMEOUT_SOCKET);

SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
schReg.register(new Scheme("https",
SSLSocketFactory.getSocketFactory(), 443));
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(
params, schReg);
DefaultHttpClient customHttpClient = new DefaultHttpClient(conMgr,
params);

customHttpClient.addRequestInterceptor(new HttpRequestInterceptor() {

public void process(
final HttpRequest request,
final HttpContext context) throws HttpException,
IOException {
if (!request.containsHeader("Accept-Encoding")) {
request.addHeader("Accept-Encoding", "gzip");
}
}

});

customHttpClient.addResponseInterceptor(new HttpResponseInterceptor() {

public void process(
final HttpResponse response,
final HttpContext context) throws HttpException,
IOException {
HttpEntity entity = response.getEntity();
Header ceheader = entity.getContentEncoding();
if (ceheader != null) {
HeaderElement[] codecs = ceheader.getElements();
for (int i = 0; i < codecs.length; i++) {
if (codecs[i].getName().equalsIgnoreCase("gzip")) {
response.setEntity(
new GzipDecompressingEntity(response.
getEntity()));
return;
}
}
}
}

});
DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(3, true);
customHttpClient.setHttpRequestRetryHandler(retryHandler);
/*		switch (checkNetworkType(EdjApp.getInstance())) {
case TYPE_CT_WAP: {
// 通过代理解决中国移动联通GPRS中wap无法访问的问题
HttpHost proxy = new HttpHost("10.0.0.200", 80, "http");
customHttpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
proxy);
Log.v("tag","当前网络类型为cm_cu_wap,设置代理10.0.0.200访问www");
}
break;
case TYPE_CM_CU_WAP: {
// 通过代理解决中国移动联通GPRS中wap无法访问的问题
HttpHost proxy = new HttpHost("10.0.0.172", 80, "http");
customHttpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
proxy);
Log.v("tag","当前网络类型为cm_cu_wap,设置代理10.0.0.172访问www");
}
break;
}*/

return customHttpClient;
}
static class GzipDecompressingEntity extends HttpEntityWrapper {

public GzipDecompressingEntity(final HttpEntity entity) {
super(entity);
}

@Override
public InputStream getContent() throws IOException,
IllegalStateException {

// the wrapped entity's getContent() decides about repeatability
InputStream wrappedin = wrappedEntity.getContent();

return new GZIPInputStream(wrappedin);
}

@Override
public long getContentLength() {
// length of ungzipped content is not known
return -1;
}

}

/***
* 判断Network具体类型(联通移动wap,电信wap,其他net)
*
* */
public static int checkNetworkType(Context mContext) {
try {
final ConnectivityManager connectivityManager = (ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo networkInfo = connectivityManager
.getActiveNetworkInfo();
if (networkInfo == null || !networkInfo.isAvailable()) {
// 注意一:
// NetworkInfo 为空或者不可以用的时候正常情况应该是当前没有可用网络,
// 但是有些电信机器,仍可以正常联网,
// 所以当成net网络处理依然尝试连接网络。
// (然后在socket中捕捉异常,进行二次判断与用户提示)。
Log.i("", "=====================>无网络");
return TYPE_NET_WORK_DISABLED;
} else {
// NetworkInfo不为null开始判断是网络类型
int netType = networkInfo.getType();
if (netType == ConnectivityManager.TYPE_WIFI) {
// wifi net处理
Log.i("", "=====================>wifi网络");
return TYPE_OTHER_NET;
} else if (netType == ConnectivityManager.TYPE_MOBILE) {
// 注意二:
// 判断是否电信wap:
// 不要通过getExtraInfo获取接入点名称来判断类型,
// 因为通过目前电信多种机型测试发现接入点名称大都为#777或者null,
// 电信机器wap接入点中要比移动联通wap接入点多设置一个用户名和密码,
// 所以可以通过这个进行判断!
final Cursor c = mContext.getContentResolver().query(
PREFERRED_APN_URI, null, null, null, null);
if (c != null) {
c.moveToFirst();
final String user = c.getString(c
.getColumnIndex("user"));
if (!TextUtils.isEmpty(user)) {
Log.i(
"",
"=====================>代理:"
+ c.getString(c
.getColumnIndex("proxy")));
if (user.startsWith(CTWAP)) {
Log.i("", "=====================>电信wap网络");
return TYPE_CT_WAP;
}
}
}
c.close();

// 注意三:
// 判断是移动联通wap:
// 其实还有一种方法通过getString(c.getColumnIndex("proxy")获取代理ip
// 来判断接入点,10.0.0.172就是移动联通wap,10.0.0.200就是电信wap,但在
// 实际开发中并不是所有机器都能获取到接入点代理信息,例如魅族M9 (2.2)等...
// 所以采用getExtraInfo获取接入点名字进行判断
String netMode = networkInfo.getExtraInfo();
Log.i("", "netMode ================== " + netMode);
if (netMode != null) {
// 通过apn名称判断是否是联通和移动wap
netMode = netMode.toLowerCase();
if (netMode.equals(CMWAP) || netMode.equals(WAP_3G)
|| netMode.equals(UNIWAP)) {
Log.i("", "=====================>移动联通wap网络");
return TYPE_CM_CU_WAP;
}
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
return TYPE_OTHER_NET;
}
return TYPE_OTHER_NET;
}
}


     

 

 二、android-async-http

 具体的使用方法在上面blog的下载中有例子,而我要说的也就是一些特殊处理。

 客户端在处理文件提交时需要考虑服务器的接收方式,现在大多的服务器由Java的三大框架构成。

在Struts2中 自定义的封装了处理文件及对象类型数据的处理。

 

<span style="font-size:14px;">	RequestParams params = new RequestParams();
AsyncHttpClient client = new AsyncHttpClient();
InputStream ff = null;
try {
ff = getResources().getAssets().open

("11.jpg",AssetManager.ACCESS_RANDOM);
//FileInputStream fin= ff;
System.out.println((ff == null) + "asdfasdffdsa");
System.out.println(ff.available() +"asdf");
byte[] bytes=new byte[ff.available()];
ff.read(bytes);
ff.close();
params.put("username", "james");
params.put("password", "123456");
params.put("file",new ByteArrayInputStream(bytes),"1.jpg");
HttpUtil.POST(client, params, null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}</span>


 

 

<span style="font-size:14px;">public static void Post(AsyncHttpClient asyncHttpClient,RequestParams params,AsyncHttpResponseHandler asyncHttpResponseHandler){
SystemOut.out("url:"+1);
asyncHttpClient.post("http://"+HOST+"/"+POST, params, asyncHttpResponseHandler);
}</span>


 

如果需要提交多个文件,就要以file[0],file[1]....这样提交,并在服务器对象方式接收文件数组。

 而服务器的接收

<span style="font-size:14px;"> private List<File> filePic;

if(filePic!=null && filePic.size()>0){
for(int i=0;i<filePic.size();i++){
long b=(System.currentTimeMillis()-1369200000000L);
String path=this.getRequest().getServletContext().getRealPath("/")+"pic/"+b+".gif";
FileUtils.copyFile(filePic.get(i), new File(path));
name+=b+".gif"+",";
Thread.sleep(50);
if(i==0){
Picture p1=new Picture();
String path1=this.getRequest().getServletContext().getRealPath("/")+"pic/"+b+".gif";
String path2=this.getRequest().getServletContext().getRealPath("/")+"pic/"+(b+1)+".gif";

boolean oo= p1.s_pic(path1, path2, 250, 350,true);
if(oo){
a.setHeadPhoto("/pic/"+(b+1)+".gif");
}

}
}
}

</span>


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