等待互联网响应时强制关闭

时间:2011-12-19 17:06:40

标签: java php android httpresponse

我的应用执行了一个HTTP请求,它将数据提交给php脚本,然后等待响应。当手机有互联网连接,但连接非常糟糕(即信号不良,或路由器没有连接到互联网)时,应用程序只需强制关闭。我怎么能阻止这个?

到目前为止,这些步骤是

  1. 人员按下提交按钮

  2. 这会启动一个异步任务,确保手机有连接(错误连接通过此)

  3. 执行HTTP Post

  4. 等待回复

  5. 所有这些步骤都围绕着try和catch来处理异常,但我仍然会强行关闭。

    这是AsyncTask代码

    private class checkDatabase extends AsyncTask<String, Void, Boolean> {
    
        ProgressDialog progressDialog;
    
        protected void onPreExecute() {
            super.onPreExecute();
            // Starting the progress dialog
            progressDialog = ProgressDialog.show(Login.this, "",
                    "Connecting. Please wait...", true);
    
        }
    
        protected Boolean doInBackground(String... info) {
            // TODO Auto-generated method stub
    
            ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
    
            if (netInfo != null && netInfo.isConnected()) {
    
                String us = info[0];
                String ps = info[1];
    
                String responseText;
    
                if (us.equals("") || ps.equals("")) {
                    //No username or password
                    return false;
                } else {
                    try {
                        // Create a new HttpClient and Post Header
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost(
                                "http://www.myfirstagent.com/android/loginquery.php");
    
                        // Add your data
                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                                2);
                        nameValuePairs.add(new BasicNameValuePair("username",
                                us));
                        nameValuePairs.add(new BasicNameValuePair("password",
                                ps));
                        httppost.setEntity(new UrlEncodedFormEntity(
                                nameValuePairs));
                        HttpResponse response;
                        // Execute HTTP Post Request
                        try {
                            response = httpclient.execute(httppost);
                            responseText = EntityUtils.toString(response
                                    .getEntity());
                        } catch (Exception e) {
                            //problem connecting
                            return false;
    
                        }
    
                        if (responseText.equals("accept")) {
                            return true;
                        } else if (responseText.equals("decline")) {
                            //invalid username or password
                            return false;
    
                        } else {
                            //something went wrong
                            return false;
                        }
    
                    } catch (Exception e) {
    
                        //something went wrong
                        return false;
                    }
                }
    
            } else {
                //No connection
                return false;
            }
    
        }
    
        protected void onPostExecute(Boolean result) {
    
            // Dismissing the progress dialog
            if (progressDialog.isShowing())
                progressDialog.dismiss();
            super.onPostExecute(result);
        }
    }
    

1 个答案:

答案 0 :(得分:0)

检查响应是否为null。

如果你想抓住一切可能发生的事情,你可以抓住一个Throwable。

了解如何阅读堆栈跟踪,这是给您最好的建议。你必须了解你的程序失败的地方,以及他们如何有机会理解他们失败的原因并纠正它们。