1

I am getting this error:

java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $

when I'm trying to get responce.

Json response I am getting is below:

[  
   {  
      "site":"bash.im",
      "name":"bash",
      "desc":"Цитатник Рунета",
      "link":"/url.html?url=http%3A%2F%2Fbash.im%2Fquote%2F445088",
      "elementPureHtml":"Text"
   },
   {  
      "site":"bash.im",
      "name":"bash",
      "desc":"Цитатник Рунета",
      "link":"/url.html?url=http%3A%2F%2Fbash.im%2Fquote%2F445087",
      "elementPureHtml":"Text"
   },
   {  
      "site":"bash.im",
      "name":"bash",
      "desc":"Цитатник Рунета",
      "link":"/url.html?url=http%3A%2F%2Fbash.im%2Fquote%2F445086",
      "elementPureHtml":"Text"
   }
]

My model class:

public class PostModel {

@SerializedName("site")
@Expose
private String site;
@SerializedName("name")
@Expose
private String name;
@SerializedName("desc")
@Expose
private String desc;
@SerializedName("link")
@Expose
private String link;
@SerializedName("elementPureHtml")
@Expose
private String elementPureHtml;

public String getSite() {
    return site;
}

public void setSite(String site) {
    this.site = site;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}

public String getLink() {
    return link;
}

public void setLink(String link) {
    this.link = link;
}

public String getElementPureHtml() {
    return elementPureHtml;
}

public void setElementPureHtml(String elementPureHtml) {
    this.elementPureHtml = elementPureHtml;
}

}

Connecting class:

    public class App extends Application {

    private static UmoriliApi umoriliApi;

    @Override
    public void onCreate() {
        super.onCreate();

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.umori.li/")
                .client(client)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
        umoriliApi = retrofit.create(UmoriliApi.class);
    }

    public static UmoriliApi getApi() {
        return umoriliApi;
    }
}

And MainActivity:

    public class MainActivity extends AppCompatActivity {

    List<PostModel> posts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        posts = new ArrayList<>();

        App.getApi().getData("bash", 50).enqueue(new Callback<List<PostModel>>() {

            @Override
            public void onResponse(Call<List<PostModel>> call, Response<List<PostModel>> response) {
                posts.addAll(response.body());
            }


            @Override
            public void onFailure(Call<List<PostModel>> call, Throwable t) {
                t.printStackTrace();
                Toast.makeText(MainActivity.this, "An error occurred during networking", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Will appreciate any help or tips.

11
  • there is an error in response that's why gson is not able to convert response to model class. Commented May 30, 2017 at 11:56
  • 1
    i think the error is in the web service response you can test the request on the browser to make sure it is matching your model Commented May 30, 2017 at 12:14
  • @Badr, I made my model according to json responce using jsonschema2pojo.org service Commented May 30, 2017 at 12:19
  • @Jaydeep Patel, could you please prompt, where exactly an error? Commented May 30, 2017 at 12:20
  • check webservice in post man . there might be problem of params or response. Commented May 30, 2017 at 12:22

1 Answer 1

7

try to clean builder build->clean project it worked

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

Comments

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.