【问题标题】:Why if condition not working in my AsyncTask onPostExecute为什么如果条件在我的 AsyncTask onPostExecute 中不起作用
【发布时间】:2014-04-17 15:16:34
【问题描述】:

我在 AsyncTask 的 doinbackground() 中得到了成功响应。当我检查 postExecute() 上的 equalsignorecase 时,它​​没有进入内部。下面是我的代码。我怎样才能进入下一步,有什么问题请帮助我。

   class Service extends AsyncTask<String, String, String>
    {
        String responseStr = null;
        String res="Failure";
        ProgressDialog pd;
        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            pd = new ProgressDialog(MainActivity.this);
            pd.setMessage("Loading..");
            pd.show();
        }
        @Override
        protected String doInBackground(String... args) {
          try {

            HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(Config.POST_URL);
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
                nameValuePairs.add(new BasicNameValuePair("loantype",mortageText));
                if(mortageText.equalsIgnoreCase("Refinanace"))
                nameValuePairs.add(new BasicNameValuePair("loanamount",h_borrow));
                nameValuePairs.add(new BasicNameValuePair("esthomevalue",estimatedHM));
                nameValuePairs.add(new BasicNameValuePair("creditscore",thirdViewBoxText));
                if(mortageText.equalsIgnoreCase("Refinanace"))
                nameValuePairs.add(new BasicNameValuePair("currentinterstrate",Integer.toString(intrest)));
                nameValuePairs.add(new BasicNameValuePair("fname",firstNameText));
                nameValuePairs.add(new BasicNameValuePair("lname",lastNameText));
                nameValuePairs.add(new BasicNameValuePair("address",streetAddressText));
                nameValuePairs.add(new BasicNameValuePair("email",emailText));
                nameValuePairs.add(new BasicNameValuePair("city",cityText));
                nameValuePairs.add(new BasicNameValuePair("state",stateText));
                nameValuePairs.add(new BasicNameValuePair("zip",zipText));
                nameValuePairs.add(new BasicNameValuePair("phone",phoneText));
                nameValuePairs.add(new BasicNameValuePair("currentyinfha",militaryText));
                nameValuePairs.add(new BasicNameValuePair("miltaryservice",militaryserviceText));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));                  
                try {
                    HttpResponse response = httpclient.execute(httppost);
                    responseStr = EntityUtils.toString(response.getEntity());

                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                    return null;
                }

             }catch (Exception e) {
                e.printStackTrace();
            }

            return responseStr;
        }
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            pd.cancel();
            if(result.equalsIgnoreCase("Success"))
            {
                dialogGreatNews();

            }

        }
    }

【问题讨论】:

  • 你有没有用Log.d()测试过result的实际内容一旦达到onPostExecute()
  • 没有,我在 doInBackGround() 中使用 Log.v() 进行了测试
  • 好的,但它是否返回预期值?
  • 空白问题。感谢您的回复。

标签: android web-services android-asynctask


【解决方案1】:

修剪空白或检查是否存在换行符

if(result.trim().equalsIgnoreCase("Success")) {

    dialogGreatNews();

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 2020-07-25
    • 2015-02-28
    • 2011-12-20
    • 2014-04-15
    相关资源
    最近更新 更多