0

数日前にこれに似た質問をしましたが、今問題に遭遇しました。以下では、asynctaskWeb サービス データを解析しています。私のWebサービスがこのデータを私のjson文字列に返すとき

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):        },      {

私はそれを解析して表示しますが、これをjson文字列に返すと

    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 0、アプリケーションは何もしません。つまり、doInBackgroundメソッドが完了すると、Web サービスがデータを返していないため、空白の画面が表示されるだけです。私が支援されたコードが存在しないように振る舞います。ですから、誰かがこれについて私を助けてくれることを望んでいました.

    protected String doInBackground(String... args) {

                try {

                    if (Integer.parseInt(json.getJSONObject("all").getString("count"))<=0){return "0";

                    }else{

                    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 TAG_QUESTION ;           

        }

        @Override
        protected void onPostExecute(String file_URL) {

            if(TAG_COUNT == null || TAG_COUNT.equals("0")){
                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);                

            }}}
4

2 に答える 2

0

doInBackgroundメソッドで結果を使用することはありませonPostExecute()ん。そのため、何も起こりません。

あなたの場合、 aをdoInBackground返し、a をパラメーターとして受け入れます ( )。StringonPostExecute()Stringfile_URL

ではdoInBackground、問題がなければ を返します。TAG_QUESTIONこれは、コードの他の場所で宣言された定数でなければなりません。失敗した場合は を返し"0"ます。したがって、 inonPostExecute()にはまたはfile_URLの値が含まれます。問題は、その内容を決してチェックしないことです。さらに、 では、 の値を変更することはありません。TAG_QUESTION"0"doInBackgroundTAG_COUNT

したがって、countnull かどうかを確認したい場合は、行を置き換える必要があります

if(TAG_COUNT == null || TAG_COUNT.equals("0")){ 

if(file_URL.equals("0")) {

それはそれを行う必要があります。

(アイテムの数だけを知りたい場合は、 inintではなくを返すのが理想的です)StringdoInBackground

于 2013-09-06T00:21:02.990 に答える