I'm making an android application for my wordpress page. I want to get several JSON data, one of them the image associated with the posts. The JSON that returns wordpress by default has the following format:
"author": 5,
"featured_media": 1836,
"comment_status": "open",
"ping_status": "open",
"sticky": false,
"format": "standard",
"meta":
With this format the GET only returns the file ID so I need to make another call to the service to get the url of the image associated with the post. How could I get the url of the image directly when I return a post from the page? Right now I'm making two calls and it's very inefficient and slow. I have tried to make changes to the plugin but it does not change the format. Any solution? I just need you to return the url of the image by doing a GET of the posts on the page.
public class FeedTask extends AsyncTask<String, Void, ArrayList<JSONArray>>{
private OnFeedListener listener;
public FeedTask(OnFeedListener listener){
this.listener=listener;
}
@Override
protected ArrayList<JSONArray> doInBackground(String... params) {
ArrayList<JSONArray> arrays=new ArrayList<>();
String url=params[0];
OkHttpClient client=new OkHttpClient();
Request.Builder builder=new Request.Builder();
Request request=builder.url(url).build();
try {
Response response=client.newCall(request).execute();
String json=response.body().string();
try{
JSONArray array= new JSONArray(json);
//Array de Imágenes
JSONArray arrayImages=new JSONArray();
for(int i=0; i<array.length(); i++) {
String urlImage=("http://MIPAGINA/wp-json/wp/v2/media/")+array.optJSONObject(i).optString("featured_media");
request=builder.url(urlImage).build();
response=client.newCall(request).execute();
json=response.body().string();
arrayImages.put(new JSONObject(json));
}
arrays.add(array);
arrays.add(arrayImages);
return arrays;
} catch (JSONException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(ArrayList<JSONArray> array) {
super.onPostExecute(array);
if(array==null){
return;
}
if(listener!=null){
listener.onFeed(array);
}
}
}