0

I am relatively new to retrofit and I have an issue in parsing some string arrays which are a part of a JSON response.

This is the JSON response.

{
"positive": [
    "Relaxed",
    "Uplifted",
    "Hungry",
    "Sleepy",
    "Tingly"
],

"medical": [
    "Eye Pressure",
    "Insomnia",
    "Stress",
    "Fatigue",
    "Headaches"
]
}

How do I approach this?

Thanks in advance :)

7
  • Create POJO class then use this link: stackoverflow.com/questions/42623437/… Commented Oct 16, 2018 at 15:55
  • List<String> maybe? Commented Oct 16, 2018 at 15:55
  • Create a POJO class which contains two list<String> Commented Oct 16, 2018 at 16:00
  • class myJsonResponse(){ List<String> positive = new List<String>; List<String> medical = new List<String>; //getters and setters } Commented Oct 16, 2018 at 16:04
  • My POJO : public List<String> getPositive() { return positive; } Commented Oct 16, 2018 at 16:11

2 Answers 2

0

By looking into your comments I assume that your POJO class is proper but the way you're using it with Retrofit call is wrong. Make sure that the POJO class is like below,

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Foo {

    @SerializedName("positive")
    @Expose
    private List<String> positive = null;
    @SerializedName("medical")
    @Expose
    private List<String> medical = null;

    public List<String> getPositive() {
        return positive;
    }

    public List<String> getMedical() {
        return medical;
    }

}

And use above POJO class like this.

Your API interface

import retrofit2.Call;
import retrofit2.http.GET;

public interface FooApiInterface {

    /* pass POJO class to Call<> */

    @GET("request/url")
    Call<Foo> getFoo(/* parameters for the request if there any */);

    /* other api calls here */
}

Next use API interface

FooApiInterface client = ...;  // initialization

Call<Foo> fooCall = client.getFoo();

fooCall.enqueue(
         new Callback<Foo>() {
              @Override
              public void onResponse(@NonNull Call<Foo> call, @NonNull Response<Foo> response) {
                 if (response.isSuccessful()) {
                     List<String> positiveList = response.body().getPositive();
                     List<String> medicalList = response.body().getMedical();
                 }
              }

              @Override
              public void onFailure(@NonNull Call<Foo> call, @NonNull Throwable t) {
                     Log.e("error", "API Error ", t);
              }
    }
);

Notice that, here I am using Foo POJO class as parameter to Call instead of List<Strain> (like used in your code). If you modify the code as said above, you can get rid of the error

java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ error

For demonstration purpose I used Foo example. Change it according to your requirements.

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

1 Comment

Works perfectly! Thanks a ton :D
0

You need to create POJO class like below

public  class  ExampleResponse  {

private  List < String >  positive  =  null;
private  List < String >  medical  =  null;

public  List < String >  getPositive() {
    return  positive;
}

public  void  setPositive(List < String >  positive) {
    this.positive  =  positive;
}

public  List < String >  getMedical() {
    return  medical;
}

public  void  setMedical(List < String >  medical) {
    this.medical  =  medical;
}

}

It's working perfectly.

3 Comments

Trying this only for positive now. My POJO : ` private List< String > positive = null; public List<String> getPositive() { return positive; }`
Main Activity : ` @Override public void onResponse(Call<List<Strain>> call, Response<List<Strain>> response) { List<Strain> basicDetailsList2 = response.body(); List<String> positiveTraitsArray = basicDetailsList2.get(0).getPositive(); Log.d("Positive array", "onResponse: " + positiveTraitsArray); } I still get the java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ error :/
Yep. @Shashanth's and your answer worked. Just had to make slight changes to my pojo. Had to make sure I had a List<String> postive.

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.