MD5 校验值:27cf44ec6e0720408f5ef017a90a3331
HttpConnector.java 文件包含反编译后的源代码,请注意,该内容仅供学习和参考使用,不得用于非法用途。
package com.gau.utils.net.connector; import android.content.Context; import com.gau.utils.net.IConnectListener; import com.gau.utils.net.INetRecord; import com.gau.utils.net.NetException; import com.gau.utils.net.asrFiltier.IAsrFilter; import com.gau.utils.net.request.THttpRequest; import com.gau.utils.net.response.IResponse; import com.gau.utils.net.util.NetLog; import com.gau.utils.net.util.NetUtil; import java.io.IOException; import java.net.SocketTimeoutException; import java.net.URI; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.apache.http.Header; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; 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.client.methods.HttpRequestBase; import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.conn.params.ConnRoutePNames; import org.apache.http.entity.ByteArrayEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; public class HttpConnector extends AbstractConnector implements Runnable { private Thread mConnectThread; private int mSwitchUriTimes; public HttpConnector(THttpRequest tHttpRequest, Context context) throws IllegalArgumentException { super(tHttpRequest, context); this.mSwitchUriTimes = 0; } public HttpConnector(THttpRequest tHttpRequest, IConnectListener iConnectListener, Context context) throws IllegalArgumentException { super(tHttpRequest, iConnectListener, context); this.mSwitchUriTimes = 0; } @Override public void connectAsynchronous() { if (this.mConnectThread == null) { this.mConnectThread = new Thread(this); this.mConnectThread.setPriority(this.mRequest.getRequestPriority()); this.mConnectThread.start(); NetLog.info("testBattery, End HttpConnector connectAsynchronous", null); } } @Override public void cancelCurrentConnect() { } @Override public void connect() { NetLog.info("testBattery, Begin HttpConnector run", null); try { this.mRequest.setCurrentUrl(this.mRequest.getUrl()); this.mConnectObserver.onStart(this.mRequest); this.mConnectObserver.onFinish(this.mRequest, connectSynchronous(this.mRequest)); } catch (NetException e) { NetLog.erro("IOException", e); e.printStackTrace(); this.mConnectObserver.onException(this.mRequest, e.mErrorCode); } catch (IOException e2) { NetLog.erro("IOException", e2); e2.printStackTrace(); this.mConnectObserver.onException(this.mRequest, 1); } catch (IllegalAccessException e3) { NetLog.erro("can't find netWork", e3); e3.printStackTrace(); this.mConnectObserver.onException(this.mRequest, 2); } catch (OutOfMemoryError e4) { e4.printStackTrace(); NetLog.erro("unkown exception ", e4); System.gc(); this.mConnectObserver.onException(this.mRequest, 4); } catch (ClientProtocolException e5) { NetLog.erro("ClientProtocolException", e5); e5.printStackTrace(); this.mConnectObserver.onException(this.mRequest, 0); } catch (Throwable th) { th.printStackTrace(); NetLog.erro("unkown exception ", th); this.mConnectObserver.onException(this.mRequest, 5); } NetLog.info("testBattery, Begin HttpConnector run", null); } @Override public void closeConnect() { } @Override public void run() { connect(); } private void configHttpHeader(HttpRequestBase httpRequestBase) { List<Header> header = this.mRequest.getHeader(); if (header != null) { int size = header.size(); for (int i = 0; i < size; i++) { httpRequestBase.addHeader(header.get(i)); } } } private void configClient(HttpClient httpClient) throws IllegalAccessException { int netWorkType = NetUtil.getNetWorkType(this.mContext); if (2 == netWorkType) { httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, NetUtil.getProxy(this.mContext)); } else if (netWorkType == -1) { throw new IllegalAccessException(); } httpClient.getParams().setParameter("http.socket.timeout", Integer.valueOf(this.mRequest.getSocketTimeoutValue())); httpClient.getParams().setParameter("http.connection.timeout", Integer.valueOf(this.mRequest.getTimeoutValue())); } private void shutDown(DefaultHttpClient defaultHttpClient) { if (defaultHttpClient != null) { NetLog.debug("shutDown client ", null); defaultHttpClient.getConnectionManager().shutdown(); } } private URI selectURI(THttpRequest tHttpRequest) { if (tHttpRequest == null || tHttpRequest.getAllUrl() == null || this.mSwitchUriTimes >= tHttpRequest.getAllUrl().size()) { return null; } return tHttpRequest.getAllUrl().get(this.mSwitchUriTimes); } private IResponse connectSynchronous(THttpRequest tHttpRequest) throws ClientProtocolException, IOException, IllegalAccessException, Exception { IResponse connectSynchronous; URI selectURI; HttpResponse execute; NetLog.info("StartConnect url= " + tHttpRequest.getUrl(), null); NetLog.info("testBattery, Begin HttpConnector connectSynchronous url = " + tHttpRequest.getUrl(), null); DefaultHttpClient defaultHttpClient = new DefaultHttpClient(); long currentTimeMillis = System.currentTimeMillis(); INetRecord netRecord = tHttpRequest.getNetRecord(); try { selectURI = selectURI(tHttpRequest); } catch (Exception e) { if (netRecord != null) { netRecord.onException(e, null, null); } int curRetryTime = tHttpRequest.getCurRetryTime(); if (curRetryTime > 0) { tHttpRequest.setCurRetryTime(curRetryTime - 1); connectSynchronous = connectSynchronous(tHttpRequest); } else { this.mSwitchUriTimes++; if (this.mSwitchUriTimes >= tHttpRequest.getAllUrl().size()) { if (e instanceof SocketTimeoutException) { throw new NetException(11); } if (e instanceof ConnectTimeoutException) { throw new NetException(12); } throw e; } connectSynchronous = connectSynchronous(tHttpRequest); } } finally { shutDown(defaultHttpClient); tHttpRequest.setCurrentUrl(null); } if (selectURI == null) { throw new NetException(6); } tHttpRequest.setCurrentUrl(selectURI); HttpHost httpHost = new HttpHost(selectURI.getHost(), selectURI.getPort()); configClient(defaultHttpClient); int protocol = tHttpRequest.getProtocol(); if (protocol == 0 || (protocol == -1 && tHttpRequest.getPostData() == null)) { HttpRequestBase httpGet = new HttpGet(selectURI); configHttpHeader(httpGet); if (netRecord != null) { netRecord.onStartConnect(tHttpRequest, null, null); } execute = defaultHttpClient.execute(httpHost, httpGet); if (netRecord != null) { netRecord.onConnectSuccess(tHttpRequest, null, null); } } else { HttpPost httpPost = new HttpPost(selectURI); configHttpHeader(httpPost); if (tHttpRequest.getPostData() != null) { ByteArrayEntity byteArrayEntity = new ByteArrayEntity(tHttpRequest.getPostData()); byteArrayEntity.setChunked(false); httpPost.setEntity(byteArrayEntity); } else { HashMap<String, String> paramMap = tHttpRequest.getParamMap(); if (paramMap != null && !paramMap.isEmpty()) { ArrayList arrayList = new ArrayList(); for (String str : paramMap.keySet()) { arrayList.add(new BasicNameValuePair(str, paramMap.get(str))); } UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(arrayList, "UTF-8"); urlEncodedFormEntity.setChunked(false); httpPost.setEntity(urlEncodedFormEntity); } } if (netRecord != null) { netRecord.onStartConnect(tHttpRequest, null, null); } execute = defaultHttpClient.execute(httpHost, httpPost); if (netRecord != null) { netRecord.onConnectSuccess(tHttpRequest, null, null); } } int statusCode = execute.getStatusLine().getStatusCode(); long currentTimeMillis2 = System.currentTimeMillis(); NetLog.info("connectTime= " + (currentTimeMillis2 - currentTimeMillis), null); NetLog.debug("responseCode= " + statusCode, null); if (statusCode == 200) { IAsrFilter asrFilter = tHttpRequest.getAsrFilter(); if (asrFilter == null || !asrFilter.isAsrResponse(execute)) { connectSynchronous = tHttpRequest.getOperator().operateHttpResponse(tHttpRequest, execute); if (netRecord != null) { netRecord.onTransFinish(tHttpRequest, null, null); } NetLog.info("DataTrafficTime= " + (System.currentTimeMillis() - currentTimeMillis2), null); return connectSynchronous; } NetLog.debug("find AsrResponse", null); int curRetryTime2 = tHttpRequest.getCurRetryTime(); if (curRetryTime2 > 0) { tHttpRequest.setCurRetryTime(curRetryTime2 - 1); connectSynchronous = connectSynchronous(tHttpRequest); } else { this.mSwitchUriTimes++; if (this.mSwitchUriTimes >= tHttpRequest.getAllUrl().size()) { throw new NetException(10); } connectSynchronous = connectSynchronous(tHttpRequest); } } else { if (statusCode == 503) { tHttpRequest.setCurRetryTime(0); } int curRetryTime3 = tHttpRequest.getCurRetryTime(); if (curRetryTime3 > 0) { tHttpRequest.setCurRetryTime(curRetryTime3 - 1); connectSynchronous = connectSynchronous(tHttpRequest); } else { this.mSwitchUriTimes++; if (this.mSwitchUriTimes >= tHttpRequest.getAllUrl().size()) { throw new NetException(statusCode); } connectSynchronous = connectSynchronous(tHttpRequest); } } NetLog.info("testBattery, end HttpConnector connectSynchronous url = " + tHttpRequest.getUrl(), null); return connectSynchronous; } }