【发布时间】:2011-09-27 07:58:44
【问题描述】:
哪种方式是建立 HTTP 连接的最佳方式。我的意思是使用代理等等。现在我正在使用这个:
StringBuilder entity = new StringBuilder();
entity.append("request body");
AndroidHttpClient httpClient = AndroidHttpClient.newInstance(null);
String proxyHost = android.net.Proxy.getDefaultHost();
int proxyPort = android.net.Proxy.getDefaultPort();
if (proxyHost != null && proxyPort > 0) {
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
ConnRouteParams.setDefaultProxy(httpClient.getParams(), proxy);
}
HttpPost httpPost = new HttpPost("https://w.qiwi.ru/term2/xmlutf.jsp");
httpPost.setEntity(new StringEntity(entity.toString(), "UTF-8"));
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 15000);
HttpConnectionParams.setSoTimeout(params, 30000);
httpPost.setParams(params);
HttpResponse httpResponse = httpClient.execute(httpPost);
int responseCode = httpResponse.getStatusLine().getStatusCode();
if (responseCode == HttpStatus.SC_OK) {
// parsing response
}
我不确定这是否可以,因为我的一位客户告诉我,他在 APN 设置中设置代理后立即出现 IllegalArgumentException。
【问题讨论】:
-
@blackbelt 我在 AsyncTask 中使用此代码
-
@LalitPoptani 我没有发现任何使用代理的地方。如果在您的安卓手机中,您的 APN 设置中有代理怎么办?
-
@nixan 抱歉不知道这个... :(
标签: android proxy httpconnection