从wordpress WP-REST APi获取图像URL

时间:2017-10-02 07:47:50

标签: android json wordpress rest android-volley

我正面临使用

从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标记中。我怎么能得到那个?

1 个答案:

答案 0 :(得分:-1)

我认为它已经晚了但也许它可以帮助一些人 我使用这些代码获取图片:

private class GetContacts extends AsyncTask<Void, Void, Void> {
    private  String url2 = "your web site/wp/wp-json/wp/v2/media/?page=1;

    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url2, ServiceHandler.GET);
        if (jsonStr.length() > 4000) {
            Log.v("jsonStr", "sb.length = " + jsonStr.length());
            int chunkCount = jsonStr.length() / 4000;     // integer division
            for (int i = 0; i <= chunkCount; i++) {
                int max = 4000 * (i + 1);
                if (max >= jsonStr.length()) {
                    Log.v("a", "chunk " + i + " of " + chunkCount + ":" + jsonStr.substring(4000 * i));
                } else {
                    Log.v("b", "chunk " + i + " of " + chunkCount + ":" + jsonStr.substring(4000 * i, max));
                }
            }
        } else {
            Log.v("shoo", jsonStr.toString());
            Log.v("bbbb", "sb.length = " + jsonStr.length());
            endOfData=true;
        }

        if (jsonStr != null) {
            try {
                JSONArray jsonObj = new JSONArray(jsonStr);
                // Getting JSON Array node
                contacts = jsonObj.length();
                allurl.clear();
                // looping through All Contacts
                for (int i = 0; i < contacts; i++) {
                    HashMap<String, String> imgurl = new HashMap<String, String>();
                    JSONObject c = jsonObj.getJSONObject(i).getJSONObject("media_details").getJSONObject("sizes");
                    if(c.has("thumbnail")) {  String thumb = c.getJSONObject("thumbnail").getString("source_url");
                        imgurl.put("thum",thumb); }else{ imgurl.put("thum","thum-null");}

                    if(c.has("medium")) {  String medium = c.getJSONObject("medium").getString("source_url");
                        imgurl.put("medi",medium);}else{ imgurl.put("medi","medi-null");}

                    if(c.has("full")) {   String full = c.getJSONObject("full").getString("source_url");
                        imgurl.put("ful",full);}else{ imgurl.put("ful","ful-null");}
                    Log.v("iiiiiiii",imgurl.toString());
                    if(c.has("thumbnail")|| c.has("medium" ) ||c.has("full")){
                        allurl.add(imgurl);
                    }
                }
            }
            catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        if( allurl != null){if (getActivity()!=null) {
            final String th[]=new String[allurl.size()];
            final String medi[]=new String[allurl.size()];
            final String ful[]=new String[allurl.size()];
            for(int i=0;i< allurl.size();i++ ){
                 if(allurl.get(i).get("thum") != "thum-null"){

                    th[i]=allurl.get(i).get("thum");
                    thu3.add(allurl.get(i).get("thum"));}else{

                    if(allurl.get(i).get("thum") != "medi-null"){
                        th[i]=allurl.get(i).get("medi");
                        thu3.add(allurl.get(i).get("medi"));
                    }else{
                        th[i]=allurl.get(i).get("ful");
                        thu3.add(allurl.get(i).get("medi"));
                    }
                }
                //medi[i]=(allurl.get(i).get("medi") != "medi-null")? medi[i]=allurl.get(i).get("medi") :(allurl.get(i).get("ful") !="ful-null") ? medi[i]=allurl.get(i).get("ful") :(medi[i]=allurl.get(i).get("thum")) ;

                medi3.add((allurl.get(i).get("medi") != "medi-null")? medi[i]=allurl.get(i).get("medi") :(allurl.get(i).get("ful") !="ful-null") ? medi[i]=allurl.get(i).get("ful") :(medi[i]=allurl.get(i).get("thum")));

                //ful[i]=(allurl.get(i).get("ful") !="ful-null")? ful[i]=allurl.get(i).get("ful") :(allurl.get(i).get("medi") != "medi-null") ? ful[i]=allurl.get(i).get("medi") :(ful[i]=allurl.get(i).get("thum")) ;

                ful3.add((allurl.get(i).get("ful") !="ful-null")? ful[i]=allurl.get(i).get("ful") :(allurl.get(i).get("medi") != "medi-null") ? ful[i]=allurl.get(i).get("medi") :(ful[i]=allurl.get(i).get("thum")));
               }



        }   }
    }

}