1

I'm having trouble loading want to retrieve url data from JSON below

{
     "status": "success",
     "message": "This is a message",
     "item":{
         "id":"1",
         "video":{
             "url" : [
                 "https://url1.com",
                 "https://url1.com",
                 "https://url1.com"
             ]
          }
     }
}

ResponseData

public class ResponseData {

    @SerializedName("status")
    private String status;
    @SerializedName("message")
    private String message;
    @SerializedName("item")
    private Item item;

    //Getters
}

Item

public class Item {
   
   @SerializedName("id")
   private String id;
   @SerializedName("video")
   private Video video;
   
   //Getters
}

Video

public class Video { 

   @SerializedName("url")
   private List<String> urlList;
   
   //Getters
}

What should I do after this to get each URL and apply it in Retrofit onResponse?

 @Override
 public void onResponse(@NonNull Call<ResponseData> call, @NonNull Response<ResponseData> response) {
     if (response.isSuccessful()) {
         ResponseData resp = response.body();

         //For the call I want results like the method below
         DataFromServer d = new DataFromServer();
         d.url = resp.getItem().getVideo().getUrlList().getUrl1(); // I want it like this
     }
 }
6
  • What exactly you want? You are getting the response, do you want to return the urls? Commented Aug 20, 2021 at 5:03
  • I want to get the url one by one, but the getUrlList() method in Video Class cannot be called because it is a List data type, whereas I want to get that method and then call it with the getUrl1() method to call it in retrofit response Commented Aug 20, 2021 at 5:11
  • assuming data is parsed properly then you can use getUrlList().get(1); Commented Aug 20, 2021 at 5:29
  • Not getting you properly, but If you want each url one by one then use for loop and use index no. Like this getUrlList().get(i); here i indicate index no. Commented Aug 20, 2021 at 5:33
  • I tried this getUrlList().get(i); But the value obtained is always null Commented Aug 20, 2021 at 5:47

1 Answer 1

1
    resp.getItem().getVideo().getUrlList().forEach((url) -> {
       Log.i("url", url) // you can get each url here
     }
    );

you are basically getting a list from server.

you don't need keys to access them. you can store them in list and also do

list.get(0) - for first url

list.get(1) - for second url

and so on.

Sign up to request clarification or add additional context in comments.

2 Comments

forEach is Kotlin method, OPs code is in Java, above won't compile/build

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.