MD5 校验值:27cf44ec6e0720408f5ef017a90a3331
HttpConnectorAlive.java 文件包含反编译后的源代码,请注意,该内容仅供学习和参考使用,不得用于非法用途。
package com.gau.utils.net.connector; import android.content.Context; import android.os.Handler; import android.os.Looper; import android.util.Log; 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.operator.IHttpOperator; import com.gau.utils.net.request.THttpRequest; import com.gau.utils.net.response.IResponse; import com.gau.utils.net.util.HeartSetting; 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.net.URISyntaxException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; 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.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.protocol.HTTP; public class HttpConnectorAlive extends AbstractConnector { private static final long ALIVE_LIFT_TIME = 180000; private Thread mConnectThread; private List<IConnectListener> mConnectorListenerList; private IConnectorDestroyListener mDestroyListener; private Handler mHandler; private IConnectListener mHeartListener; private THttpRequest mHeartRequest; private Runnable mHeartRunnable; private HeartSetting mHeartSetting; private HttpClient mHttpClient; private boolean mIsCloseConnect; private List<THttpRequest> mRequestList; private int mSwitchUriTimes; private Object mSyncObject; private Object mTag; private long mTimeStamp; public interface IConnectorDestroyListener { void onConnectorDestroy(AbstractConnector abstractConnector); } public HttpConnectorAlive(THttpRequest tHttpRequest, Context context, IConnectorDestroyListener iConnectorDestroyListener) { super(tHttpRequest, context); this.mHttpClient = null; this.mSwitchUriTimes = 0; this.mIsCloseConnect = false; this.mSyncObject = new Object(); this.mTimeStamp = 0L; this.mHeartRunnable = new Runnable() { @Override public void run() { HttpConnectorAlive.this.addHeartRequest(); if (HttpConnectorAlive.this.mHandler != null) { HttpConnectorAlive.this.mHandler.removeCallbacks(this); } } }; this.mDestroyListener = iConnectorDestroyListener; init(); } public HttpConnectorAlive(THttpRequest tHttpRequest, IConnectListener iConnectListener, Context context, IConnectorDestroyListener iConnectorDestroyListener) throws IllegalArgumentException { super(tHttpRequest, iConnectListener, context); this.mHttpClient = null; this.mSwitchUriTimes = 0; this.mIsCloseConnect = false; this.mSyncObject = new Object(); this.mTimeStamp = 0L; this.mHeartRunnable = new Runnable() { @Override public void run() { HttpConnectorAlive.this.addHeartRequest(); if (HttpConnectorAlive.this.mHandler != null) { HttpConnectorAlive.this.mHandler.removeCallbacks(this); } } }; this.mDestroyListener = iConnectorDestroyListener; init(); } public void setHeartSetting(HeartSetting heartSetting) { this.mHeartSetting = heartSetting; } public void addRequest(THttpRequest tHttpRequest, IConnectListener iConnectListener) { synchronized (this.mSyncObject) { long currentTimeMillis = System.currentTimeMillis(); if (!tHttpRequest.equals(this.mHeartRequest)) { this.mTimeStamp = currentTimeMillis; if (this.mRequestList.contains(this.mHeartRequest)) { this.mRequestList.remove(this.mHeartRequest); } if (this.mHandler != null) { this.mHandler.removeCallbacks(this.mHeartRunnable); } } else if (currentTimeMillis - this.mTimeStamp > ALIVE_LIFT_TIME) { closeConnect(); if (this.mDestroyListener != null) { this.mDestroyListener.onConnectorDestroy(this); } return; } this.mRequestList.add(tHttpRequest); Collections.sort(this.mRequestList, new Comparator<THttpRequest>() { @Override public int compare(THttpRequest tHttpRequest2, THttpRequest tHttpRequest3) { if (tHttpRequest2.getRequestPriority() < tHttpRequest3.getRequestPriority()) { return 1; } if (tHttpRequest2.getRequestPriority() > tHttpRequest3.getRequestPriority()) { return -1; } return 0; } }); this.mConnectorListenerList.add(this.mRequestList.indexOf(tHttpRequest), iConnectListener); if (this.mIsCloseConnect) { this.mIsCloseConnect = false; } if (this.mHeartRequest != null) { this.mHeartRequest.setHeartTime(tHttpRequest.getHeartTime()); } this.mSyncObject.notifyAll(); } } public List<THttpRequest> getRequestList() { return this.mRequestList; } public int getRequestSize() { if (this.mRequestList != null) { return this.mRequestList.size(); } return 0; } public void setTag(Object obj) { this.mTag = obj; } public Object getTag() { return this.mTag; } public long getIdleTime() { return System.currentTimeMillis() - this.mTimeStamp; } public void removeRequest(THttpRequest tHttpRequest, IConnectListener iConnectListener) { synchronized (this.mSyncObject) { if (this.mRequestList != null && this.mRequestList.size() > 0) { this.mRequestList.remove(tHttpRequest); } if (this.mConnectorListenerList != null && this.mConnectorListenerList.size() > 0) { this.mConnectorListenerList.remove(iConnectListener); } this.mSyncObject.notifyAll(); } } private void init() { this.mTimeStamp = System.currentTimeMillis(); this.mHttpClient = new DefaultHttpClient(); this.mRequestList = new ArrayList(); this.mConnectorListenerList = new ArrayList(); addRequest(this.mRequest, this.mConnectObserver); initHeartListener(); initHeartRequest(this.mRequest); configClient(this.mRequest, this.mHttpClient); } private void initHeartListener() { if (this.mHeartListener == null) { this.mHeartListener = new IConnectListener() { @Override public void onStart(THttpRequest tHttpRequest) { } @Override public void onFinish(THttpRequest tHttpRequest, IResponse iResponse) { } @Override public void onException(THttpRequest tHttpRequest, int i) { Log.i("ABEN", "HttpConnectorAlive initHeartListener onException reason = " + i); HttpConnectorAlive.this.closeConnect(); if (HttpConnectorAlive.this.mDestroyListener != null) { HttpConnectorAlive.this.mDestroyListener.onConnectorDestroy(HttpConnectorAlive.this); } } @Override public void onException(THttpRequest tHttpRequest, HttpResponse httpResponse, int i) { onException(tHttpRequest, i); } }; } } private String selectHeartUrl(THttpRequest tHttpRequest) { String str = null; if (this.mRequest.getHeartUrl() != null) { str = this.mRequest.getHeartUrl().toString(); } if (str == null && this.mHeartSetting != null && this.mRequest.getCurrentUrl() != null) { str = this.mHeartSetting.getHeartUrl(this.mRequest.getCurrentUrl().getHost()); } if (str == null) { return this.mRequest.getUrl().toString(); } return str; } public void addHeartRequest() { if (!this.mRequestList.contains(this.mHeartRequest)) { String selectHeartUrl = selectHeartUrl(this.mRequest); if (selectHeartUrl != null) { try { this.mHeartRequest.setUrl(selectHeartUrl); } catch (URISyntaxException e) { e.printStackTrace(); } } addRequest(this.mHeartRequest, this.mHeartListener); } } private void initHeartRequest(THttpRequest tHttpRequest) { if (this.mHeartRequest == null) { try { this.mHeartRequest = new THttpRequest(selectHeartUrl(tHttpRequest), null, this.mHeartListener); this.mHeartRequest.setOperator(new IHttpOperator() { @Override public IResponse operateHttpResponse(THttpRequest tHttpRequest2, HttpResponse httpResponse) throws IllegalStateException, IOException { return null; } }); this.mHeartRequest.setNetRecord(new INetRecord() { @Override public void onTransFinish(THttpRequest tHttpRequest2, Object obj, Object obj2) { } @Override public void onStartConnect(THttpRequest tHttpRequest2, Object obj, Object obj2) { } @Override public void onException(Exception exc, Object obj, Object obj2) { } @Override public void onConnectSuccess(THttpRequest tHttpRequest2, Object obj, Object obj2) { } }); this.mHeartRequest.setHeartTime(tHttpRequest.getHeartTime()); this.mHeartRequest.setRequestPriority(1); } catch (Exception e) { } } } private void configClient(THttpRequest tHttpRequest, HttpClient httpClient) { if (tHttpRequest != null && httpClient != null) { if (2 == NetUtil.getNetWorkType(this.mContext)) { httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, NetUtil.getProxy(this.mContext)); } httpClient.getParams().setParameter("http.socket.timeout", Integer.valueOf(tHttpRequest.getSocketTimeoutValue())); httpClient.getParams().setParameter("http.connection.timeout", Integer.valueOf(tHttpRequest.getTimeoutValue())); } } private void setConnect() { 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 connect() { } public void handleConnect() { if (!this.mRequestList.isEmpty() && !this.mConnectorListenerList.isEmpty()) { this.mRequest = this.mRequestList.remove(0); this.mConnectObserver = this.mConnectorListenerList.remove(0); if (this.mRequest != null && this.mConnectObserver != null) { if (!this.mRequest.equals(this.mHeartRequest)) { this.mIsIdle = false; } else { this.mIsIdle = true; } setConnect(); this.mIsIdle = true; } } } @Override public void connectAsynchronous() { if (this.mHandler == null) { this.mHandler = new Handler(Looper.getMainLooper()); } if (this.mConnectThread == null) { this.mConnectThread = new Thread(new Runnable() { @Override public void run() { while (!HttpConnectorAlive.this.mIsCloseConnect) { synchronized (HttpConnectorAlive.this.mSyncObject) { if (HttpConnectorAlive.this.mRequestList.isEmpty()) { HttpConnectorAlive.this.mHandler.postDelayed(HttpConnectorAlive.this.mHeartRunnable, HttpConnectorAlive.this.selectHeartTime()); try { HttpConnectorAlive.this.mSyncObject.wait(); } catch (InterruptedException e) { } } } HttpConnectorAlive.this.handleConnect(); } HttpConnectorAlive.this.mConnectThread = null; } }, "AliveConnectorConnectAsynchronous"); this.mConnectThread.start(); } } public long selectHeartTime() { long heartTime = this.mRequest.getHeartTime(); if (heartTime == -1) { heartTime = this.mHeartSetting.getCommonHeartTime(); } if (heartTime == -1) { return HeartSetting.DEFAULT_HEART_TIME_INTERVAL; } return heartTime; } @Override public void closeConnect() { if (this.mHttpClient != null) { try { this.mHttpClient.getConnectionManager().shutdown(); } catch (Exception e) { e.printStackTrace(); } this.mHttpClient = null; } if (this.mHandler != null) { this.mHandler.removeCallbacks(this.mHeartRunnable); } this.mIsCloseConnect = true; } @Override public void cancelCurrentConnect() { } private IResponse connectSynchronous(THttpRequest tHttpRequest) throws ClientProtocolException, IOException, IllegalAccessException, Exception { IResponse connectSynchronous; URI selectURI; HttpResponse httpResponse; NetLog.info("StartConnect url= " + tHttpRequest.getUrl(), null); NetLog.info("testBattery, Begin HttpConnector connectSynchronous url = " + tHttpRequest.getUrl(), null); 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 { this.mSwitchUriTimes = 0; } if (selectURI == null) { throw new NetException(6); } tHttpRequest.setCurrentUrl(selectURI); HttpHost httpHost = new HttpHost(selectURI.getHost(), selectURI.getPort()); if (this.mHttpClient == null) { this.mHttpClient = new DefaultHttpClient(); } configClient(tHttpRequest, this.mHttpClient); if (tHttpRequest.getPostData() == null) { HttpRequestBase httpGet = new HttpGet(selectURI); configHttpHeader(httpGet); configAliveHeader(httpGet); if (netRecord != null) { netRecord.onStartConnect(tHttpRequest, null, null); } HttpResponse execute = this.mHttpClient.execute(httpHost, httpGet); if (netRecord != null) { netRecord.onConnectSuccess(tHttpRequest, null, null); } httpResponse = execute; } else { HttpPost httpPost = new HttpPost(selectURI); configHttpHeader(httpPost); configAliveHeader(httpPost); ByteArrayEntity byteArrayEntity = new ByteArrayEntity(tHttpRequest.getPostData()); byteArrayEntity.setChunked(false); httpPost.setEntity(byteArrayEntity); if (netRecord != null) { netRecord.onStartConnect(tHttpRequest, null, null); } HttpResponse execute2 = this.mHttpClient.execute(httpHost, httpPost); if (netRecord != null) { netRecord.onConnectSuccess(tHttpRequest, null, null); } httpResponse = execute2; } int statusCode = httpResponse.getStatusLine().getStatusCode(); if (statusCode == 200) { IAsrFilter asrFilter = tHttpRequest.getAsrFilter(); if (asrFilter == null || !asrFilter.isAsrResponse(httpResponse)) { connectSynchronous = tHttpRequest.getOperator().operateHttpResponse(tHttpRequest, httpResponse); httpResponse.getEntity().consumeContent(); if (netRecord != null) { netRecord.onTransFinish(tHttpRequest, null, 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; } 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 configAliveHeader(HttpRequestBase httpRequestBase) { httpRequestBase.addHeader(HTTP.CONN_DIRECTIVE, "keep-alive"); } private URI selectURI(THttpRequest tHttpRequest) { if (tHttpRequest != null && this.mSwitchUriTimes == 0 && tHttpRequest.getAllUrl() != null && this.mSwitchUriTimes < tHttpRequest.getAllUrl().size()) { return tHttpRequest.getAllUrl().get(this.mSwitchUriTimes); } return null; } }