0

onPostExecute在我的应用程序中,如果我的 Web 服务不返回任何数据,我必须执行某种方法。我在我的一个旧问题中遇到了这样的问题,您可以在此处看到onPostExecute 导致应用程序崩溃现在,如果您在该链接中阅读我的问题,那么您就会知道当我的应用程序返回值"0"时它崩溃了。但是现在我的问题不同了,每当我的 Web 服务确实返回计数时,"0"我的应用程序都会显示一个空白布局,而不是执行我希望它执行的方法。有人可以花很长时间来 onPostExecute帮助我解决这个问题。

               final String TAG_RESULTS = "results";
 final String TAG_QUESTION_SUBJECT = "Subject";
 final String TAG_QUESTION_NUMANSWERS = "NumAnswers";
 final String TAG_QUESTION = "question";
 final String TAG_QUESTION_CONTENT = "Content";
 final String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer";
 final String TAG_ANSWERS = "Answers";
 final String TAG_ANSWER = "Answer";    
 final String TAG_ANSWERS_CONTENT = "content";      
 final String TAG_QUERY = "query";
 final String TAG_COUNT = "count";
 ProgressDialog pDialog;
 LoadAllData mTask; 

……

受保护的字符串doInBackground(字符串... args){

            pDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){ 
                public void onCancel(DialogInterface dialog) { 
                    mTask.cancel(true); 
                    finish(); 
            } 
       }); 

            try {
                Intent in = getIntent();
                String searchTerm = in.getStringExtra("TAG_SEARCH");
                String query = URLEncoder.encode(searchTerm, "utf-8");
                String URL = "http://example.com";
                JSONParsser jParser = new JSONParsser();
                JSONObject json = jParser.readJSONFeed(URL);
                try {

                    JSONArray questions = json.getJSONObject("all").getJSONArray("questions");

                    for(int i = 0; i < questions.length(); i++) {
                        JSONObject question = questions.getJSONObject(i);


                    String Subject = question.getString(TAG_QUESTION_SUBJECT);
                    String ChosenAnswer = question.getString(TAG_QUESTION_CHOSENANSWER);
                    String Content = question.getString(TAG_QUESTION_CONTENT);

                           HashMap<String, String> map = new HashMap<String, String>();

                           map.put(TAG_QUESTION_SUBJECT, Subject);
                           map.put(TAG_QUESTION_CONTENT, Content);
                           map.put(TAG_QUESTION_CHOSENANSWER, ChosenAnswer);

                           questionList.add(map);

                }


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

                return null;       

    }



@Override
    protected void onPostExecute(String file_URL) {

        if(file_URL!=null && file_URL.equals("0")) {
            pDialog.dismiss();
            Toast.makeText(ListView.this, "No data found", Toast.LENGTH_SHORT).show();
            finish();

        } else {

            if (pDialog != null && pDialog.isShowing()) 
                pDialog.dismiss();
            ListAdapter adapter = new SimpleAdapter(getBaseContext(), questionList,
                    R.layout.listelements,
                    new String[] { TAG_QUESTION_SUBJECT }, new int[] {
                    R.id.Subject,});

            setListAdapter(adapter);                

        }
    }

这是数据返回时的样子count "0"

09-02 00:25:55.466: D/JSON String(882): {
09-02 00:25:55.466: D/JSON String(882): "all" : {
09-02 00:25:55.466: D/JSON String(882):     "count" : 0,
09-02 00:25:55.466: D/JSON String(882):     "questions" : [     ]
09-02 00:25:55.466: D/JSON String(882):     }
09-02 00:25:55.466: D/JSON String(882): }

这是数据在返回数据或有效时的样子count

08-30 00:36:07.917: D/JSON String(1891): {
08-30 00:36:07.917: D/JSON String(1891): "all" :    {
08-30 00:36:07.917: D/JSON String(1891):    "count" : 25,
08-30 00:36:07.917: D/JSON String(1891):    "questions" : [     {   
08-30 00:36:07.917: D/JSON String(1891):            "Id" : "20100728112033AAb4hTA",
08-30 00:36:07.917: D/JSON String(1891):            "Subject" : "What is the oldest a bitch can more or less safely breed?",
08-30 00:36:07.917: D/JSON String(1891):            "Content" : "Don't worry I'm not going to breed - both my bitches are getting neutered in the next year, as is my dog! Just that me and a friend were talking about it after talking to the man who gave me Misty (my border collie) who said she could still breed at 7 - I thought the oldest was 5?\n",
08-30 00:36:07.917: D/JSON String(1891):            "Date" : "2010-07-28 18:20:33",
08-30 00:36:07.917: D/JSON String(1891):            "Timestamp" : "1280341233",
08-30 00:36:07.917: D/JSON String(1891):            "Link" : "http://answers.yahoo.com/question/?qid=20100728112033AAb4hTA",
08-30 00:36:07.917: D/JSON String(1891):            "Type" : "Answered",
08-30 00:36:07.917: D/JSON String(1891):            "CategoryId" : 396546021,
08-30 00:36:07.917: D/JSON String(1891):            "CategoryName" : "Dogs",
08-30 00:36:07.917: D/JSON String(1891):            "UserId" : "cP16Ctgxaa",
08-30 00:36:07.917: D/JSON String(1891):            "UserNick" : "Kiko",
08-30 00:36:07.917: D/JSON String(1891):            "UserPhotoURL" : "http://l.yimg.com/dg/users/1t1USxJpxAAEBQOGZjBMW0-5Wp_EG.medium.jpg",
08-30 00:36:07.917: D/JSON String(1891):            "NumAnswers" : 8,
08-30 00:36:07.917: D/JSON String(1891):            "NumComments" : 0,
08-30 00:36:07.917: D/JSON String(1891):            "ChosenAnswer" : "You just \"imagined\" that ,dear...not \"thought\". & your imagination is WRONG.\r\n\r\n9 or even 10,for a healthy TOP-PRODUCING bitc-h.\r\n\r\n\r\n\r\n*&* bitches are SPAYED & dogs are CASTRATED......big scary correct ADULT words.",
08-30 00:36:07.917: D/JSON String(1891):            "ChosenAnswererId" : "clN6YITvaa",
08-30 00:36:07.917: D/JSON String(1891):            "ChosenAnswererNick" : "Debunker",
08-30 00:36:07.917: D/JSON String(1891):            "ChosenAnswerTimestamp" : "1280317077",
08-30 00:36:07.917: D/JSON String(1891):            "ChosenAnswerAwardTimestamp" : "1280835336"
08-30 00:36:07.917: D/JSON String(1891):        },      {

屏幕为空白的原因是因为它正在尝试填充我的列表视图但计数为零,所以这就是它为空白的原因。我只需要知道为什么它if(file_URL!=null && file_URL.equals("0")) { pDialog.dismiss(); Toast.makeText(ListView.this, "No data found", Toast.LENGTH_SHORT).show(); finish();返回时不会执行count "0"

4

1 回答 1

0

每当我的 Web 服务确实返回计数“0”时,我的应用程序都会显示一个空白布局,而不是执行我希望它执行的方法。

如果它返回0,那么将运行的唯一代码是

    pDialog.dismiss();
    Toast.makeText(ListView.this, "No data found", Toast.LENGTH_SHORT).show();
    finish();

据我所知,除了关闭你的ProgressDialog,显示 a Toast,然后完成你的Activity.

于 2013-10-07T19:50:41.443 回答