我遇到有关使用wordpress获取图像的问题
WP-Rest-API
这是wordpress的JSON
结构
> [{"id":2107,"date":"2017-09-28T07:32:09","date_gmt":"2017-09-28T07:32:09","guid":{"rendered":"http:\/\/localhost:8080\/diygeeks\/?p=2107"},"modified":"2017-09-28T07:32:09","modified_gmt":"2017-09-28T07:32:09","slug":"flute-test","status":"publish","type":"post","link":"http:\/\/localhost:8080\/diygeeks\/2017\/09\/28\/flute-test\/","title":{"rendered":"Flute
> Test"},"content":{"rendered":"<figure id=\"attachment_2108\"
> style=\"width: 300px\" class=\"wp-caption alignnone\"><img
> class=\"size-medium wp-image-2108\"
> src=\"http:\/\/localhost:8080\/diygeeks\/wp-content\/uploads\/2017\/09\/flute_player_ii_by_uitarifd-300x210.jpg\" alt=\"error\" width=\"300\" height=\"210\"
> srcset=\"http:\/\/localhost:8080\/diygeeks\/wp-content\/uploads\/2017\/09\/flute_player_ii_by_uitarifd-300x210.jpg
> 300w,
> http:\/\/localhost:8080\/diygeeks\/wp-content\/uploads\/2017\/09\/flute_player_ii_by_uitarifd-768x538.jpg
> 768w,
> http:\/\/localhost:8080\/diygeeks\/wp-content\/uploads\/2017\/09\/flute_player_ii_by_uitarifd-1024x717.jpg
> 1024w,
> http:\/\/localhost:8080\/diygeeks\/wp-content\/uploads\/2017\/09\/flute_player_ii_by_uitarifd-545x382.jpg
> 545w,
> http:\/\/localhost:8080\/diygeeks\/wp-content\/uploads\/2017\/09\/flute_player_ii_by_uitarifd.jpg
> 1068w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/>
ImageURL
在html img
标记中。我该怎么办? 最佳答案
我认为已经晚了,但也许可以帮助一些人
我使用以下代码获取图片:
为了获取图像URL,您应该具有JsonObject和jsonArray的基本知识。
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
List <myImageHolderObject> list ;
public void ImageListMaker(String js){
try{
JSONArray jArray = new JSONArray(js); //get ASrray of items
int Jlength = jArray.length();
for (int i = 0; i < Jlength; i++) { //loop throw evry item and get image url
JSONObject jo = jArray.getJSONObject(i);
int id = jo.getInt("id");
JSONObject c =jo.getJSONObject("media_details").getJSONObject("sizes");
String thumb = c.getJSONObject("thumbnail").getString("source_url");
String medium = c.getJSONObject("medium").getString("source_url");
String full = c.getJSONObject("full").getString("source_url");
list.add(new myImageHolderObject(id,thumb,medium,full));
}
}catch(JSONException e){
e.printStackTrace();
}
}