0
我想研究一下Android中使用的內置apache版本,以及是否有任何可以從apache網站上更新最新穩定apache jar的版本。目前我遇到的問題是在執行發佈請求時相當大的延遲。我已經閱讀了很多關於它的stackoverflow的帖子,並且有些用戶說從版本4.1.1開始這個問題就解決了。我試圖把最新的穩定的apache客戶端和核心罐子放到我的項目中,將它們添加到依賴關係中,但似乎仍然使用android內置的apache。如何強制使用第三方牙膏瓶?Android內置的apache vs最新的穩定的apache
我的HttpClient:
import com.example.restservice.service.EasySSLSocketFactory;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import java.security.KeyStore;
public class HttpUtils {
private static DefaultHttpClient client;
public static HttpClient getHttpClient() {
if (client != null)
return client;
try {
// here we can set a custom keystore if we need to
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new EasySSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
client = new DefaultHttpClient(ccm, params);
} catch (Exception e) {
client = new DefaultHttpClient();
}
return client;
}
}
SSLSoket:
import org.apache.http.conn.ssl.SSLSocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
public class EasySSLSocketFactory extends SSLSocketFactory {
SSLContext sslContext = SSLContext.getInstance("TLS");
public EasySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
super(truststore);
TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sslContext.init(null, new TrustManager[] {
tm
}, null);
}
@Override
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
}
@Override
public Socket createSocket() throws IOException {
return sslContext.getSocketFactory().createSocket();
}
}
謝謝,我已經試過這個庫,但它並沒有幫助我,儘管它有最後一個穩定的apache版本。我發現自己只有一個合乎邏輯的答案 - 我試圖在本地服務器上執行請求,但安裝在不太好的硬件上。因爲當我嘗試在整個活動服務器中執行相同的請求時,所有內容都會像短槍一樣被解僱。 – user1376885 2013-04-11 06:08:49
如果您懷疑服務器問題,您應該從圖片中刪除android,並使用curl/wget來確保您的服務器在必要時進行響應。然後去Android客戶端 – 2013-04-11 14:13:03
檢查兩個服務器捲曲。幾乎相同的結果〜兩個500-700ms。如果通過android發送請求,那麼可能相當於~500-幾分鐘或更長時間的活和本地服務器。 – user1376885 2013-04-11 14:40:00