Java tutorial
/** * Copyright 2013 Alibaba.com All right reserved. * This software is the confidential and proprietary information of Alibaba.com ("Confidential Information"). * You shall not disclose such Confidential Information and shall use it only in accordance with the terms of the license agreement you entered into with Alibaba.com. * * @(#) AbstractRequest.java */ package com.floyd.http; import java.io.IOException; import java.util.List; import java.util.Map; import org.apache.http.Header; import android.os.Handler; import android.text.TextUtils; import android.util.Log; /** * ?http,{@link AbstractRequest#execute()} <br> * * @author floydchenxf */ public abstract class AbstractRequest extends AbstractWebUtils { private static final String sTAG = "AbstractRequest"; protected static int SO_CONNECT_TIMEOUT = 10000;//10 SECONDS protected static int SO_READ_TIMEOUT = 30000;//30 SECONDS protected String url; protected HttpMethod httpMethod; protected List<Header> headers; protected Map<String, String> parameters; protected Map<String, FileItem> attachments; protected Handler callbackHandler; protected Callback callback; protected Object requestFlag; protected AbstractRequest() { } /** * @param url * @param parameters * @param httpMethod */ public AbstractRequest(String url, Map<String, String> parameters, HttpMethod httpMethod) { this(url, null, parameters, null, httpMethod, null, null); } /** * @param url * @param headers * @param parameters * @param httpMethod */ public AbstractRequest(String url, List<Header> headers, Map<String, String> parameters, HttpMethod httpMethod) { this(url, headers, parameters, null, httpMethod, null, null); } /** * ? * * @param url ? * @param headers * @param parameters * @param attachments * @param httpMethod * @param callback * @param requestFlag ?{@code callback}??requestcallback?????request?null */ public AbstractRequest(String url, List<Header> headers, Map<String, String> parameters, Map<String, FileItem> attachments, HttpMethod httpMethod, Callback callback, Object requestFlag) { if (TextUtils.isEmpty(url)) { throw new IllegalArgumentException("url must not empty"); } this.url = url; this.headers = headers; this.parameters = parameters; this.attachments = attachments; this.callback = callback; this.requestFlag = requestFlag; this.httpMethod = httpMethod; } /** * ???? * * @return */ protected AbstractRequest decorateRequest() { return this; } /** * ??api<br/> * requestcallback?{@link Callback#onCompleted(Response, Object)}?<br> * ?{@code Response}Response?http response code ?200?<br> * ????????<br> * * @param request * @return ???,?http{@link Response#isSuccess()} false */ public Response execute() { Response response = null; try { decorateRequest(); switch (getHttpMethod()) { case POST: response = doPost(url, parameters, headers, attachments, SO_CONNECT_TIMEOUT, SO_READ_TIMEOUT); break; case GET: response = doGet(url, parameters, headers, DEFAULT_CHARSET, SO_CONNECT_TIMEOUT, SO_READ_TIMEOUT); break; default: return response; } //Log.v(sTAG, ""+response); if (response == null) { throw new IOException("response is null"); } } catch (Exception e1) { Log.e(sTAG, "exception: " + e1.getMessage()); if (response == null) { response = Response.fromError(e1, null); } else { response.setRequestError(new RequestError(e1, null)); } } if (this.callback != null) { final Response resp = response; final AbstractRequest req = this; // do callback Handler callbackHandler = req.getCallbackHandler(); if (callbackHandler == null) { // Run on this thread. req.getCallback().onCompleted(resp, req.getRequestFlag()); } else { // Post to the handler. callbackHandler.post(new Runnable() { public void run() { req.getCallback().onCompleted(resp, req.getRequestFlag()); } }); } } return response; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public List<Header> getHeaders() { return headers; } public void setHeaders(List<Header> headers) { this.headers = headers; } public Map<String, String> getParameters() { return parameters; } public void setParameters(Map<String, String> parameters) { this.parameters = parameters; } /** * @return the attachments */ public Map<String, FileItem> getAttachments() { return attachments; } /** * @param attachments the attachments to set */ public void setAttachments(Map<String, FileItem> attachments) { this.attachments = attachments; } public Callback getCallback() { return callback; } public void setCallback(Callback callback) { this.callback = callback; } public Object getRequestFlag() { return requestFlag; } public void setRequestFlag(Object requestFlag) { this.requestFlag = requestFlag; } public HttpMethod getHttpMethod() { return httpMethod; } public void setHttpMethod(HttpMethod httpMethod) { this.httpMethod = httpMethod; } public Handler getCallbackHandler() { return callbackHandler; } public void setCallbackHandler(Handler callbackHandler) { this.callbackHandler = callbackHandler; } //=========================================================== // inner class //=========================================================== /** * @author floydchenxf */ public interface Callback { void onCompleted(Response response, Object requestFlag); } }