0

几天前我问了一个类似的问题,但现在我遇到了一个问题。在asynctask下面,我正在解析 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

1

我建议您使用Gson来自动解析文档。您可以为 json 对象创建相应的 java 类。整数、字符串值可以按原样映射。Json 可以这样解析:

Gson gson = new Gson(); 
Response r = gson.fromJson(jsonString, Response.class);

您在此链接中有一个完整的示例

于 2013-09-06T09:05:14.927 回答
0

你永远不会doInBackground在你的onPostExecute()方法中使用结果,这就是为什么什么都没有发生。

在您的情况下,doInBackground返回 a String,并onPostExecute()接受 aString作为参数 ( file_URL)。

doInBackground中,如果没问题,你 return TAG_QUESTION,它必须是在你的代码其他地方声明的常量,当它失败时,你 return "0"。因此, inonPostExecute()file_URL包含 或 的TAG_QUESTION"0"。问题是你从不检查它的内容。此外,在 中doInBackground,您永远不会更改 的值TAG_COUNT

因此,如果要检查是否count为空,则应替换该行

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

经过

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

那应该这样做。

(理想情况下,如果您只想知道项目的数量,最好返回 anint而不是 a Stringin )doInBackground

于 2013-09-06T00:21:02.990 回答