我正在尝试使用HttpPost登录服务器(在我的HTC One V上进行测试)。我得到了
org.apache.http.conn.ConnectTimeoutException: Connect to /121.241.197.102:15500 timed out
但奇怪的是它可以通过手机的浏览器轻松访问它。我有3G连接,我可以在一秒钟内从浏览器访问URL,我已将超时参数设置为20秒。
我用来连接的代码:
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 200000);
HttpConnectionParams.setSoTimeout(params, 200000);
httpClient= new DefaultHttpClient(params);
CookieStore cookieStore = httpClient.getCookieStore();
if(cookies.size()>0)
for(int i=0;i<cookies.size();i++)
cookieStore.addCookie(cookies.get(i));
httpClient.setCookieStore(cookieStore);
HttpResponse httpResponse;
InputStream inputStream=null;
try{
HttpPost httpPost=new HttpPost(url);
StringEntity se=new StringEntity(input.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(se);
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Accept", "application/json");
httpResponse = httpClient.execute(httpPost);
.
.
.
.
.
.
.
尝试执行(httpPost)时连接失败。在模拟器上,我尝试使用完全相同的代码和本地网络IP,它可以完美地使用相同的代码。当我使用公共IP和真实设备时出现问题。任何解决方案?
修改 这是LogCat
06-19 10:47:44.559: W/System.err(2466): org.apache.http.conn.ConnectTimeoutException: Connect to /121.241.197.102:15500 timed out
06-19 10:47:44.569: W/System.err(2466): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
06-19 10:47:44.569: W/System.err(2466): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
06-19 10:47:44.569: W/System.err(2466): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-19 10:47:44.569: W/System.err(2466): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-19 10:47:44.569: W/System.err(2466): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-19 10:47:44.569: W/System.err(2466): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-19 10:47:44.589: W/System.err(2466): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-19 10:47:44.589: W/System.err(2466): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
06-19 10:47:44.589: W/System.err(2466): at com.ambr.tradeapp.utility.JSONConnectorPost.connectClient(JSONConnectorPost.java:66)
06-19 10:47:44.589: W/System.err(2466): at com.ambr.tradeapp.activity.MainActivity$LoginAsyncTask.doInBackground(MainActivity.java:82)
06-19 10:47:44.589: W/System.err(2466): at com.ambr.tradeapp.activity.MainActivity$LoginAsyncTask.doInBackground(MainActivity.java:1)
06-19 10:47:44.599: W/System.err(2466): at android.os.AsyncTask$2.call(AsyncTask.java:288)
06-19 10:47:44.599: W/System.err(2466): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-19 10:47:44.599: W/System.err(2466): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-19 10:47:44.599: W/System.err(2466): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-19 10:47:44.599: W/System.err(2466): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-19 10:47:44.599: W/System.err(2466): at java.lang.Thread.run(Thread.java:841)