0
我正在致力於將新聞發佈到wordpress網站的新聞發佈到android fone應用程序。 我可以從JSON陣列測試(標題,日期,內容),但我不能從陣列獲取圖像的URL任何想法從wordpress json api獲取json數組的圖片url
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);
}
});
}
}
`
發佈您的JSON示例 –