2

我最近在我的 android 应用程序中重构了一些网络代码,以使用 Google 推荐的HTTPUrlConnection。以前,我使用的是 Apache 的 HTTPClient。我不确定这些事情是否相关。

我最近对我的网络代码所做的另一件事是使用 AsyncTask 进行调用。以前我只是在主线程上工作(显然很糟糕),所以我的应用程序在获取数据时似乎会挂起。问题是,自从切换到 AsyncTask 后,我经常遇到超时错误。

为什么我会收到此超时错误?

 java.net.ConnectException: failed to connect to <url> (port 80): connect failed: ETIMEDOUT (connection timed out)

这是我的 AsyncTask 进行网络调用。

private class PostToPHP extends AsyncTask<PostToPHP, Void, String>
{
    private String functionName;
    private ArrayList<NameValuePair> postKeyValuePairs;

    public PostToPHP(String function, ArrayList<NameValuePair> keyValuePairs)
    {
        functionName= function;
        postKeyValuePairs = keyValuePairs;
    }

    @Override
    protected void onPreExecute()
    {
        progressDialog = ProgressDialog.show(BaseActivity.getInstance(), "Loading", "Please wait...", true, false);
    }

    @Override
    protected String doInBackground(PostToPHP... params)
    {
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair(FUNCTION_NAME, functionName));
        for (int i = 0; i < postKeyValuePairs.size(); i++) {
            nameValuePairs.add(postKeyValuePairs.get(i));
        }

        try {
            URL url = new URL("www.myurl");
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setDoOutput(true);
            urlConnection.setRequestProperty("Accept-Charset", iso-8859-1);
            OutputStream os = urlConnection.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, iso-8859-1));
            writer.write(getEncodedPostParameters(nameValuePairs, iso-8859-1));
            writer.close();
            os.close();

            InputStream is = urlConnection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, iso-8859-1));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            urlConnection.disconnect();

            return sb.toString();
        }
        catch (UnknownHostException e) {
            // handle it

            return null;
        }
        catch (Exception e) {
            // this is where I'm always getting a timeout exception
            return null;
        }
    }

    @Override
    protected void onPostExecute(String result)
    {
        progressDialog.dismiss();
    }
}

编辑:我认为这只发生在一个特定的网络通话中,但我现在已经在不同的地方经历过。

编辑2:再看一遍我发现了这个,我认为这更多是我的问题。在我为网络调用实现 Android AsyncTask 之前,这并不存在。我认为 AsyncTask 在某种程度上搞砸了线程并导致超时。

4

2 回答 2

1

您可以显式设置 HTTPUrlConnection 的超时时间:
urlConnection.setConnectionTimeout(aLongerTimeout)

于 2013-03-10T22:53:56.717 回答
1

我对这个答案不是很满意,但它似乎奏效了。我认为我的代码一定有问题,或者 HTTPUrlConnection 中存在某种错误。我知道谷歌说它在 Froyo 和更低版本中存在错误,但我看到我正在测试的 2.3.4 和 4.2 中有一些奇怪的地方。本质上,我用 Apache 的代码替换了上面的代码,但我没有看到超时。

private static String post(ArrayList<NameValuePair> params){        
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httpreq = new HttpPost("www.url.com");
    httpreq.setEntity(new UrlEncodedFormEntity(params));
    HttpResponse response = httpclient.execute(httpreq);
    HttpEntity entity = response.getEntity();
    InputStream is = entity.getContent();

    BufferedReader reader = new BufferedReader(new InputStreamReader(is, NetworkConstants.HTTP_ACCEPTED_CHARSET), 8);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    is.close();          

    return sb.toString();
}
于 2013-03-10T23:55:01.900 回答