为什么我会得到Android:目标服务器无法响应?

时间:2014-07-26 11:15:08

标签: android api http httpresponse

API链接:http://www.familybuds.com/storegrunt-new/api_home/validate_step_one/platform/1

POST参数:store_name = "abc"代码在GoDaddy服务器上

这个api工作正常到7月25日,但突然间它从26日早上在Android APP上停止工作。同样的api在iPhone APP上工作正常。

Android代码:

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 10000);
HttpConnectionParams.setSoTimeout(httpParameters, 10000+12000);

DefaultHttpClient  client = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost(url);

HttpResponse response; 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  

client.addRequestInterceptor(new HttpRequestInterceptor() {              

        @Override
        public void process(HttpRequest request, HttpContext context)
                throws HttpException, IOException {
            // TODO Auto-generated method stub
            if (!request.containsHeader("Accept-Encoding")) {
                request.addHeader("Accept-Encoding", "gzip");
            }
        }

    });

client.addResponseInterceptor(new HttpResponseInterceptor() {

        public void process(
                final HttpResponse response,
                final HttpContext context) throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                Header ceheader = entity.getContentEncoding();
                if (ceheader != null) {
                    HeaderElement[] codecs = ceheader.getElements();
                    for (int i = 0; i < codecs.length; i++) {
                        if (codecs[i].getName().equalsIgnoreCase("gzip")) {
                            response.setEntity(
                                    new GzipDecompressingEntity(response.getEntity()));
                            return;
                        }
                    }
                }
            }
        }               


    });

// Log.d(TAG, "Executing request: "+ methodToString(mMethod) +": "+ mUri.toString());
response = client.execute(httppost);

Android的堆栈跟踪(日志):

07-26 16:24:28.031: W/System.err(13642): org.apache.http.NoHttpResponseException: The target server failed to respond
07-26 16:24:28.089: W/System.err(13642):    at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:85)
07-26 16:24:28.097: W/System.err(13642):    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
07-26 16:24:28.097: W/System.err(13642):    at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:179)
07-26 16:24:28.101: W/System.err(13642):    at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
07-26 16:24:28.105: W/System.err(13642):    at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
07-26 16:24:28.105: W/System.err(13642):    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
07-26 16:24:28.105: W/System.err(13642):    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
07-26 16:24:28.105: W/System.err(13642):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:421)
07-26 16:24:28.109: W/System.err(13642):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-26 16:24:28.113: W/System.err(13642):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-26 16:24:28.113: W/System.err(13642):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-26 16:24:28.117: W/System.err(13642):    at com.webhelper.WebApiRequest.performJSon(WebApiRequest.java:111)
07-26 16:24:28.121: W/System.err(13642):    at com.webhelper.HttpHelper.doInBackground(HttpHelper.java:171)
07-26 16:24:28.121: W/System.err(13642):    at com.webhelper.HttpHelper.doInBackground(HttpHelper.java:1)
07-26 16:24:28.124: W/System.err(13642):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-26 16:24:28.124: W/System.err(13642):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
07-26 16:24:28.128: W/System.err(13642):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
07-26 16:24:28.128: W/System.err(13642):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
07-26 16:24:28.128: W/System.err(13642):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
07-26 16:24:28.132: W/System.err(13642):    at java.lang.Thread.run(Thread.java:1019)

Https connection Android尝试过解决方案,但没有运气..

我尝试在Amazon EC2 Server上安装相同的服务器代码..并且API与Android和iPhone APP配合良好。

任何人都可以提供帮助,这可能是导致Android应用突然无法在GoDaddy上与服务器(API)进行通信的原因

1 个答案:

答案 0 :(得分:4)

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 10000);
HttpConnectionParams.setSoTimeout(httpParameters, 10000+12000);


DefaultHttpClient  client = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost(url);

尝试使用此

替换上述代码
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 10000);
HttpConnectionParams.setSoTimeout(httpParameters, 10000+12000);


DefaultHttpClient  client = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httpost.setParams(httpParameters);

这个解决方案对我有用......