0

I'm trying to retrieve data from a third-party API but I'm having this issue down below:

 Process: com.main.instareeldownloader, PID: 8483
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:225)

This is where I'm trying to retrieve data.

StringRequest request = new StringRequest(URL, response -> {
        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();

        MainUrl mainUrl = gson.fromJson(response, MainUrl.class);
        reeelUrl = mainUrl.getItems();

        Log.e("Data",reeelUrl);
        uri2 = Uri.parse(reeelUrl);
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(uri2);
        videoView.requestFocus();
        videoView.start();
    }, error -> Toast.makeText(getContext(), "Not able to Fetch.", Toast.LENGTH_SHORT).show());

    RequestQueue queue = Volley.newRequestQueue(getContext());
    queue.add(request);

This is my MainUrl Model

String items;
//private MainReel items;

public MainUrl(String items) {
    this.items = items;
}

public String getItems() {
    return items;
}

public void setItems(String items) {
    this.items = items;
}

@Override
public String toString() {
    return "MainUrl{" +
            "items='" + items + '\'' +
            '}';
}

JSON format

From the JSON try to retrieve this:

items -> video_versions -> url

I'm confused why an error is occurring for.

Uncle Roger
  • 183
  • 12
  • The JSON you receive contains a String (`"..."`), but you are expecting an Object (`{ ... }`) when you try to parse it as `MainUrl`. – juzraai Apr 21 '22 at 05:50
  • But I'm trying to get a String called Items – Uncle Roger Apr 21 '22 at 05:56
  • From a MainUrl Object, but the code fails earlier, because the JSON you got cannot be parsed to MainUrl. That's what the error says. Print out the actual JSON and see for yourself what you got from the server. – juzraai Apr 21 '22 at 06:23
  • You can check it from [here](https://www.instagram.com/reel/CNsBB9zBmdBnxgz0QETH07jVLIhOvtS9ar0UlE0/?__a=1) – Uncle Roger Apr 21 '22 at 08:45

0 Answers0