从wordpress json api获取json数组中的图像URL

时间:2014-01-30 10:33:17

标签: android json wordpress

正在研究新的阅读器,即将新闻发布到wordpress网站到android fone应用程序。 我可以从json数组中获得测试(标题,日期,内容),但我无法从数组中获取任何想法的图像网址

   class LoadBrkNews extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Breaking.this);
        pDialog.setMessage("Loading News Feeds...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting breaking JSON
     * */
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        // getting JSON string from URL
        JSONObject json = jsonParser.makeHttpRequest(BREAK_URL, "GET",
                params);

        // Check your log cat for JSON reponse
        Log.d("BREAKING NEWS JSON: ", json.toString());

        try {
            if (json.getString("status").equalsIgnoreCase("ok")) {
                breakFeeds = json.getJSONArray("posts");
                //breakImg = json.getJSONArray(TAG_IMAGE_URL);


                // looping through All messages
                for (int i = 0; i < breakFeeds.length(); i++) {
                    JSONObject c = (JSONObject) breakFeeds.getJSONObject(i);
                //  JSONObject forImg = (JSONObject) breakImg.getJSONObject(i);
                    // Storing each json item in variable
                    String id = c.getString(TAG_ID);
                    String title = c.getString(TAG_TITLE);
                    String content = c.getString(TAG_CONTENT);
                    String date = c.getString(TAG_DATE);
                    // remember to remove this line fro production
                    String urlForImage = c.getString(TAG_IMAGE_URL);

                    Log.d("attachemtent url",urlForImage);
                    // Strip off tags

                      String fcontent = content.replaceAll("<(.*?)\\>"," ");
                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_ID, id);
                    map.put(TAG_TITLE, title);
                    map.put(TAG_CONTENT, fcontent);
                    map.put(TAG_DATE, date);

                    map.put(TAG_IMAGE_URL, urlForImage);

                    // adding HashList to ArrayList
                    breakingNewsList.add(map);

                    ObjectOutput out = new ObjectOutputStream(
                            new FileOutputStream(tempFile));
                    String toCache = breakFeeds.toString();
                    out.writeObject(toCache);
                    out.close();
                    Log.d("Write to Cache", "Success");
                }

            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(Breaking.this,
                        breakingNewsList, R.layout.imagelayout,
                        new String[] { TAG_TITLE, TAG_CONTENT, TAG_DATE,
                                TAG_IMAGE_URL }, new int[] { 
                                R.id.title, R.id.content, R.id.date,R.id.imglink });
                // updating listview
                setListAdapter(adapter);
            }
        });

    }

}

`

1 个答案:

答案 0 :(得分:0)

由于您只使用图片网址时出现问题,我猜它可能也是  1.您的数据缺少图片网址  2. TAG_IMAGE_URL与数据中的密钥不匹配。

尝试使用一些json解析库来简化操作。