【问题标题】:How to set a connection timeout and add an AlertDialog for it如何设置连接超时并为其添加 AlertDialog
【发布时间】:2014-10-19 13:32:52
【问题描述】:

我在 AsyncTask innerc 类中创建以下方法以连接到 Internet 并从提供的 url 获取 JSON 文件。如果连接不好,我会遇到问题,我的应用会挂起很长时间等待连接...

        private JSONArray connectAndCreateJsonArray(String url) {
            JSONArray jsonArray = new JSONArray();

            try {
                HttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet(url);
                request.addHeader("Cache-Control", "no-cache");
                HttpResponse response = client.execute(request);
                HttpEntity entity = response.getEntity();

                InputStreamReader in = new InputStreamReader(entity.getContent());
                BufferedReader reader = new BufferedReader(in);
                StringBuilder stringBuilder = new StringBuilder();
                String line = "";
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line);
                }

                jsonArray = new JSONArray(stringBuilder.toString());

            } catch (IllegalStateException e) {
                e.printStackTrace();                
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return jsonArray;
        }

我从 doInBackground() 方法调用此方法。如何使用 AlertDialog 处理连接超时?

【问题讨论】:

    标签: android android-asynctask connection timeout android-alertdialog


    【解决方案1】:
    HttpGet httpGet = new HttpGet(url);
    HttpParams httpParameters = new BasicHttpParams();
    

    在您的连接建立之前超时

    int timeOutInMillis = 10*1000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeOutInMillis);
    

    设置超时时间,直到收到您的数据

    int socketTimeOutinMillis = 5000;
    HttpConnectionParams.setSoTimeout(httpParameters, socketTimeOutinMillis);
    

    使用参数创建客户端

    DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
    HttpResponse response = httpClient.execute(httpGet);
    

    【讨论】:

    • 工作,谢谢!由于 NIPHIN 的回答,我为其添加了 AlertDialog。使用您的代码,我在 10 秒或 15 秒后得到 AlertDialog?
    • 取决于你到底想要什么。如果你想在 15 秒后设置一个 connecitonTimeout:int timeOutInMillis = 15*1000;如果您希望您的数据在 5 秒后到达您的客户端: int socketTimeOutinMillis = 5000;只需设置您喜欢的毫秒数。顺便说一句:0 毫秒意味着默认超时!
    • 你误会了,对不起。我的意思是,如果我将第一次超时设置为 5 秒,将第二次超时设置为 5 秒,那么总等待时间是 5 秒还是 10 秒?在您的示例中,总等待时间是 10 秒还是 15 秒?
    • 那么您的客户端有 5 秒的时间连接到您的服务器,并有 5 秒的时间来获取数据。但这并不意味着他有超过10秒。如果他需要 2 秒来连接,他仍然只有 5 秒来获取数据。
    • 在最后一种情况下,如果没有连接到互联网,AlertDialog 会在 5 或 10 秒后出现?
    【解决方案2】:

    我猜你正试图在你的 mainUI 线程上连接到互联网,这会导致你的应用程序挂起。为您的连接生成一个单独的线程。您可以捕获超时异常并在相应的捕获块中显示 toast 或 alertdialog

    catch (TimeoutException e) {

          String error = e.getMessage();    
            e.printStackTrace();  
             boolean errorflag=true;      
          return error ;      
    

    在执行后向用户显示此异常

    【讨论】:

    • 我在 AsyncTask 内部类中调用我的方法。
    • 如前所述在 catch 块中使用 timeoutexception
    • TimeoutException 无法到达的 catch 块。 try 语句体中永远不会抛出此异常
    • 这里没有证据表明在主线程上进行了连接:事实上这会抛出一个自己的NetworkingOnMainThreadException。 '超时异常'是SocketTimeoutException,除非HttpClient 有它自己的一个。 -1
    • 它似乎工作,但我不能在 doInBackground() 中使用 Toast 或 AlertDialog...如何解决这个问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    • 2014-11-18
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 2013-01-24
    相关资源
    最近更新 更多