0

私は Android の初心者で、URL から JSON フィードをダウンロードして解析する短いプログラムを作成しています。ダウンロードには AsyncTask を使用します。

doInBackground() 部分はうまく機能しているようです。次に、ブレークポイントを onPostExecute() に設定し、で停止することもできparseJSON(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");
            }
    }
4

2 に答える 2

1

JSONObject.getString()文字列型の値を取得しようとしますが、必要なのは配列型です。

于 2012-10-25T02:36:30.517 に答える
0

JSON配列を正しく取得できなかったと思います。はjson_obj.getString、配列ではなく文字列を提供します。

次のように変更してみてください。

  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");
            }
    }
于 2012-10-25T01:04:36.710 に答える