Back to project page FrameLite.
The source code is released under:
GNU General Public License
If you think the Android project FrameLite listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.miku.framelite.api.http; //from ww w. j av a 2 s. co m import java.io.IOException; import java.net.MalformedURLException; import java.net.SocketTimeoutException; import java.util.HashMap; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.HttpVersion; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.conn.params.ConnManagerParams; 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 com.miku.framelite.api.BaseRequest; import com.miku.framelite.api.RetResult; import com.miku.framelite.utils.Log; /** * * Http????? * * @author xr.lee * * @param <T> * ??????????? * @param <V> * ???????????????V???????????????T */ public abstract class AbstractHttpRequest<T, V> extends BaseRequest<T, V> { private static final String TAG = AbstractHttpRequest.class.getSimpleName(); private static final int DEFAULT_TIMEOUT = 8 * 1000; private HashMap<String, String> headers = new HashMap<String, String>(); /** * ?????{@link HttpType#POST}?{@link HttpType#GET} * * @return */ protected abstract HttpType getHttpType(); /** * * @return ???url */ protected abstract String getUrl(); /** * ???????HttpEntity * * @return */ protected abstract HttpEntity getBody(); /** * ??????rawHttpEntity???????????V,???onParse????? * * @param rawHttpEntity * @return ????????V * @throws Exception */ protected abstract V handleRawContent(HttpEntity rawHttpEntity) throws Exception; /** * ???? * * @return ??????????? */ protected int getTimeOut() { return DEFAULT_TIMEOUT; } @Override public V doInBackground(RetResult<T> result) { isCancelled=false; HttpClient httpClient = createHttpClient(); HttpRequestBase request = null; if (getHttpType() == HttpType.GET) { request = new HttpGet(getUrl()); } else { HttpPost rq = new HttpPost(getUrl()); if (getBody() != null) { rq.setEntity(getBody()); } request = rq; } addHeadersToRequest(request); try { HttpResponse response = httpClient.execute(request); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { return handleRawContent(response.getEntity()); } else { result.setRetCode(response.getStatusLine().getStatusCode()); result.setMsg(response.getStatusLine().toString()); Log.e(TAG, response.getStatusLine().toString()); } } catch (SocketTimeoutException e) { e.printStackTrace(); result.setRetCode(RetResult.RET_ERROR_TIMEOUT); result.setMsg("Socket Timeout:" + e.getMessage()); } catch (ConnectTimeoutException e) { e.printStackTrace(); result.setRetCode(RetResult.RET_ERROR_TIMEOUT); result.setMsg("Connect Timeout:" + e.getMessage()); } catch (MalformedURLException e) { e.printStackTrace(); result.setRetCode(RetResult.RET_ERROR_UNKNOWN); result.setMsg("Bad Url:" + getUrl() + " MalformedURLException:" + e.getMessage()); } catch (IOException e) { e.printStackTrace(); result.setRetCode(RetResult.RET_ERROR_UNKNOWN); result.setMsg("IOException:" + e.getMessage()); } catch (Exception e) { e.printStackTrace(); result.setRetCode(RetResult.RET_ERROR_UNKNOWN); result.setMsg("Exception:" + e.getMessage()); } return null; } private void addHeadersToRequest(HttpRequestBase request) { for (String name : headers.keySet()) { request.addHeader(name, headers.get(name)); } } protected void initializeHeaders() { addHeader(HTTP.CONN_CLOSE, "close"); } /** * ????? * * @param name * ?????? * @param value * ??? */ protected final void addHeader(String name, String value) { headers.put(name, value); } private HttpClient createHttpClient() { HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpConnectionParams.setTcpNoDelay(params, true); ConnManagerParams.setTimeout(params, getTimeOut()); HttpConnectionParams.setConnectionTimeout(params, getTimeOut()); HttpConnectionParams.setSoTimeout(params, getTimeOut()); return new DefaultHttpClient(params); } }