【ListViewJSON】【com.demo.app.api】【ApiClient】源码分析及其在工程中作用
源码如下:
package com.demo.app.api; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Map; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import com.demo.app.AppContext; import com.demo.app.AppException; import com.demo.app.bean.NewsList; import com.demo.app.bean.URLs; import com.demo.app.common.StringUtils; /** * API客户端接口:用于访问网络数据 * * @version 1.0 * @created 2012-3-21 */ public class ApiClient { public static final String UTF_8 = "UTF-8"; public static final String DESC = "descend"; public static final String ASC = "ascend"; private final static int TIMEOUT_CONNECTION = 8000; private final static int TIMEOUT_SOCKET = 8000; private final static int RETRY_TIME = 2; private static String appCookie; private static String appUserAgent; public static void cleanCookie() { appCookie = ""; } public static void cleanCookie(AppContext appContext) { appCookie = ""; appContext.setProperty("cookie", appCookie); } public static String getCookie(AppContext appContext) { if (appCookie == null || appCookie == "") { appCookie = appContext.getProperty("cookie"); } return appCookie; } private static String getUserAgent(AppContext appContext) { if (appUserAgent == null || appUserAgent == "") { StringBuilder ua = new StringBuilder("APP"); ua.append(‘/‘ + appContext.getPackageInfo().versionName + ‘_‘ + appContext.getPackageInfo().versionCode);// App版本 ua.append("/Android");// 手机系统平台 ua.append("/" + android.os.Build.VERSION.RELEASE);// 手机系统版本 ua.append("/" + android.os.Build.MODEL); // 手机型号 ua.append("/" + appContext.getAppId());// 客户端唯一标识 appUserAgent = ua.toString(); } return appUserAgent; } private static HttpClient getHttpClient() { HttpClient httpClient = new HttpClient(); // 设置 HttpClient 接收 Cookie,用与浏览器一样的策略 httpClient.getParams().setCookiePolicy( CookiePolicy.BROWSER_COMPATIBILITY); // 设置 默认的超时重试处理策略 httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); // 设置 连接超时时间 httpClient.getHttpConnectionManager().getParams() .setConnectionTimeout(TIMEOUT_CONNECTION); // 设置 读数据超时时间 httpClient.getHttpConnectionManager().getParams() .setSoTimeout(TIMEOUT_SOCKET); // 设置 字符集 httpClient.getParams().setContentCharset("GBk"); return httpClient; } private static GetMethod getHttpGet(String url, String cookie, String userAgent) { GetMethod httpGet = new GetMethod(url); // 设置 请求超时时间 // cookie=" CYTY%5FERP=realname=002500010103&W%5Fno=0025000101&userID=11&username=002500010103"; httpGet.getParams().setSoTimeout(TIMEOUT_SOCKET); httpGet.setRequestHeader("Host", URLs.HOST); httpGet.setRequestHeader("Connection", "Keep-Alive"); httpGet.setRequestHeader("Cookie", cookie); httpGet.setRequestHeader("User-Agent", userAgent); return httpGet; } private static PostMethod getHttpPost(String url, String cookie, String userAgent) { PostMethod httpPost = new PostMethod(url); // 设置 请求超时时间 httpPost.getParams().setSoTimeout(TIMEOUT_SOCKET); httpPost.setRequestHeader("Host", URLs.HOST); httpPost.setRequestHeader("Connection", "Keep-Alive"); httpPost.setRequestHeader("Cookie", cookie); httpPost.setRequestHeader("User-Agent", userAgent); httpPost.getParams().setParameter( HttpMethodParams.HTTP_CONTENT_CHARSET, "GBK"); return httpPost; } private static String _MakeURL(String p_url, Map<String, Object> params) { StringBuilder url = new StringBuilder(p_url); if (url.indexOf("?") < 0) url.append(‘?‘); for (String name : params.keySet()) { url.append(‘&‘); url.append(name); url.append(‘=‘); url.append(String.valueOf(params.get(name))); // 不做URLEncoder处理 // url.append(URLEncoder.encode(String.valueOf(params.get(name)), // UTF_8)); } return url.toString().replace("?&", "?"); } /** * get请求URL * * @param url * @throws AppException */ private static String http_get(AppContext appContext, String url) throws AppException { // System.out.println("get_url==> "+url); String cookie = getCookie(appContext); String userAgent = getUserAgent(appContext); HttpClient httpClient = null; GetMethod httpGet = null; String responseBody = ""; int time = 0; do { try { httpClient = getHttpClient(); httpGet = getHttpGet(url, cookie, userAgent); int statusCode = httpClient.executeMethod(httpGet); if (statusCode != HttpStatus.SC_OK) { throw AppException.http(statusCode); } responseBody = httpGet.getResponseBodyAsString(); // System.out.println("XMLDATA=====>"+responseBody); break; } catch (HttpException e) { time++; if (time < RETRY_TIME) { try { Thread.sleep(1000); } catch (InterruptedException e1) { } continue; } // 发生致命的异常,可能是协议不对或者返回的内容有问题 e.printStackTrace(); throw AppException.http(e); } catch (IOException e) { time++; if (time < RETRY_TIME) { try { Thread.sleep(1000); } catch (InterruptedException e1) { } continue; } // 发生网络异常 // e.printStackTrace(); throw AppException.network(e); } finally { // 释放连接 httpGet.releaseConnection(); httpClient = null; } } while (time < RETRY_TIME); responseBody = responseBody.replaceAll("\\p{Cntrl}", ""); return responseBody; } /** * 公用post方法 * * @param url * @param params * @param files * @throws AppException */ private static String _post(AppContext appContext, String url, NameValuePair[] data, Map<String, File> files) throws AppException { String cookie = getCookie(appContext); String userAgent = getUserAgent(appContext); HttpClient httpClient = null; PostMethod httpPost = null; String responseBody = ""; int time = 0; do { try { httpClient = getHttpClient(); httpPost = getHttpPost(url, cookie, userAgent); httpPost.setRequestBody(data); int statusCode = httpClient.executeMethod(httpPost); if (statusCode != HttpStatus.SC_OK) { throw AppException.http(statusCode); } else if (statusCode == HttpStatus.SC_OK) { Cookie[] cookies = httpClient.getState().getCookies(); String tmpcookies = ""; for (Cookie ck : cookies) { tmpcookies += ck.toString() + ";"; } // 保存cookie if (appContext != null && tmpcookies != "") { appContext.setProperty("cookie", tmpcookies); appCookie = tmpcookies; } } responseBody = httpPost.getResponseBodyAsString(); // System.out.println("XMLDATA=====>"+responseBody); break; } catch (HttpException e) { time++; if (time < RETRY_TIME) { try { Thread.sleep(1000); } catch (InterruptedException e1) { } continue; } // 发生致命的异常,可能是协议不对或者返回的内容有问题 e.printStackTrace(); throw AppException.http(e); } catch (IOException e) { time++; if (time < RETRY_TIME) { try { Thread.sleep(1000); } catch (InterruptedException e1) { } continue; } // 发生网络异常 e.printStackTrace(); throw AppException.network(e); } finally { // 释放连接 httpPost.releaseConnection(); httpClient = null; } } while (time < RETRY_TIME); responseBody = responseBody.replaceAll("\\p{Cntrl}", ""); return responseBody; } /** * post请求URL * * @param url * @param params * @param files * @throws AppException * @throws IOException * @throws */ // private static Result http_post(AppContext appContext, String url, // Map<String, Object> params, Map<String,File> files) throws AppException, // IOException { // return Result.parse(_post(appContext, url, params, files)); // } /** * 获取新闻数据列表 * * @return * @throws AppException */ public static NewsList getNewsList(AppContext appContext) throws AppException { String newUrl = URLs.NEWS_LIST; try { System.out.println("获取新闻列表:" + newUrl); return NewsList.parse(StringUtils.toJSONArray(http_get(appContext, newUrl))); } catch (Exception e) { System.out.println(e); if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * 获取网络图片 * * @param url * @return */ public static Bitmap getNetBitmap(String url) throws AppException { // System.out.println("image_url==> "+url); HttpClient httpClient = null; GetMethod httpGet = null; Bitmap bitmap = null; int time = 0; do { try { httpClient = getHttpClient(); httpGet = getHttpGet(url, null, null); int statusCode = httpClient.executeMethod(httpGet); if (statusCode != HttpStatus.SC_OK) { throw AppException.http(statusCode); } InputStream inStream = httpGet.getResponseBodyAsStream(); bitmap = BitmapFactory.decodeStream(inStream); inStream.close(); break; } catch (HttpException e) { time++; if (time < RETRY_TIME) { try { Thread.sleep(1000); } catch (InterruptedException e1) { } continue; } // 发生致命的异常,可能是协议不对或者返回的内容有问题 e.printStackTrace(); throw AppException.http(e); } catch (IOException e) { time++; if (time < RETRY_TIME) { try { Thread.sleep(1000); } catch (InterruptedException e1) { } continue; } // 发生网络异常 e.printStackTrace(); throw AppException.network(e); } finally { // 释放连接 httpGet.releaseConnection(); httpClient = null; } } while (time < RETRY_TIME); return bitmap; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。