2013-10-07 50 views
0

在我的應用程序中,我的onPostExecute執行某種方法,如果我的Web服務沒有返回任何數據。在我的一個老問題中,我遇到過這樣的問題,您可以在這裏看到onPostExecute causing application to crash現在,如果您在該鏈接中閱讀我的問題,那麼您會知道,當我的應用程序返回值"0"時,它會崩潰。但現在我的問題是不同的,每當我的Web服務確實返回計數"0"我的應用程序顯示一個空白布局,而不是執行我希望它執行的方法。有人可以在我的onPostExecute長時間,並幫助我解決這個問題。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"

回答

0

每當我的web服務不會返回數「0」我的應用程序顯示一個空白的佈局,而不是執行我想它要執行的方法。

如果返回0然後將運行的唯一代碼

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

從我所看到的,你是不是調用一個方法以外解僱你ProgressDialog,呈現出Toast,再整理您的Activity

+0

因此,無論何時您不止一次運行它?我很困惑... – codeMagic

+0

我很抱歉我以錯誤的方式解釋了我的問題......在鏈接中的舊問題中,當我的Web服務DOESNT返回count「」0「時,它導致我的應用程序崩潰。這是當我建議使用if(file_URL!= null && file_URL.equals(「0」))'來檢查Web服務是否返回'0',以便我的應用程序不會崩潰,如果Web服務不返回'0'。但是當我的網絡服務返回'0'時,它會顯示空白屏幕 –

+0

不,這就是我閱讀它的方式。我說的是我不知道你想要發生什麼,如果它* *返回「0」'。你正在調用'finish();'所以它會關閉這個'Activity'。 – codeMagic