簡體   English   中英

如何使onPostExecute()或Json解析工作?

[英]How to make onPostExecute() or Json parsing work?

我是Android的初學者,我正在編寫一個簡短的程序,以從URL下載JSON feed並對其進行解析。 我使用AsyncTask進行下載。

doInBackground()部分似乎運行良好。 然后,將斷點設置為onPostExecute(),它甚至可以在parseJSON(result)處停止,並且“ result”顯示已下載的正確json字符串。 但是,當我嘗試進入parseJSON(result) ,它將無法正確進入函數(直接拋出JSONException或進入parseJSON(result)一些隨機行)。

從DDMS日志來看,它也沒有顯示任何有價值的信息。

我如何找到問題所在? 是因為我錯誤地使用了onPostExecute()還是parseJSON()有問題?

public class MainActivity extends Activity {

    private listItem[] items;

    public class listItem {
        String title;
        String description;
        String imageHref;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        items = new listItem[50];

        new DownloadJsonFeed().execute("http://dl.dropbox.com/u/10168342/facts.json");
    }

    private class DownloadJsonFeed extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {

            try {
                return downloadUrl(params[0]);
            } catch (IOException e) {
                return "Unable to retrieve json feed. URL may be invalid.";
            }
        }

        @Override
        protected void onPostExecute(String result) {
            try {
                parseJSON(result);          // Here !!!!!!
            } catch (JSONException e) {
            }
        }
    }

    private String downloadUrl(String myurl) throws IOException {
        InputStream is = null;

        try {
            URL url = new URL(myurl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.connect();
            is = conn.getInputStream();

            // Convert the InputStream into a string
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line = null;

            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            is.close();
            return sb.toString();
        } finally {
            if (is != null) {
                is.close();
            } 
        }
    }

    private void parseJSON(String feed) throws JSONException {
            JSONObject json_obj = new JSONObject(feed);
            title = json_obj.getString("title");
            String rows = json_obj.getString("rows");

            JSONArray jArray = new JSONArray(rows);
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject tmp = jArray.getJSONObject(i);
                items[i].title = tmp.getString("title");
                items[i].description = tmp.getString("description");
                items[i].imageHref = tmp.getString("imageHref");
            }
    }

JSONObject.getString()將嘗試獲取String類型的值,但您想要的是數組類型。

我認為您沒有正確獲取JSON數組。 json_obj.getString將給您一個String而不是一個數組。

嘗試更改如下:

  private void parseJSON(String feed) throws JSONException {
            JSONObject json_obj = new JSONObject(feed);
            title = json_obj.getString("title");
            String rows = json_obj.getString("rows");

            JSONArray jArray = json_obj.getJSONArray("rows"); //<---- change this line
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject tmp = jArray.getJSONObject(i);
                items[i].title = tmp.getString("title");
                items[i].description = tmp.getString("description");
                items[i].imageHref = tmp.getString("imageHref");
            }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM