package com.mzeat; import java.io.IOException; import java.io.InputStream; 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.cookie.CookiePolicy; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.preference.PreferenceActivity.Header; import android.util.Log; 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 = 20000; private final static int TIMEOUT_SOCKET = 20000; private final static int RETRY_TIME = 3; private static String appCookie; private static String appUserAgent; /** * 获取网络图片 * * @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; } /** * 获取网络图片 * * @param url * @return */ public static Bitmap getNetBitmap(String url, int inSampleSize) 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); } org.apache.commons.httpclient.Header Content_Length = httpGet.getResponseHeader("Content-Length"); String img_length = Content_Length.getValue(); int length = Integer.valueOf(img_length); BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = inSampleSize; Log.e("img_length",img_length); InputStream inStream = httpGet.getResponseBodyAsStream(); if (length > 204800) { bitmap = BitmapFactory.decodeStream(inStream, null, options); }else { 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; } 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(UTF_8); return httpClient; } public final static String HOST = "www.mzeat.com"; private static GetMethod getHttpGet(String url, String cookie, String userAgent) { GetMethod httpGet = new GetMethod(url); // 设置 请求超时时间 httpGet.getParams().setSoTimeout(TIMEOUT_SOCKET); httpGet.setRequestHeader("Host", HOST); httpGet.setRequestHeader("Connection", "Keep-Alive"); httpGet.setRequestHeader("Cookie", cookie); httpGet.setRequestHeader("User-Agent", userAgent); return httpGet; } }