获取错误:预期BEGIN_ARRAY但是在第1行第1行路径$ STRING

时间:2017-05-30 11:55:19

标签: android json retrofit2

我收到此错误:

  

java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在第1行第1行为STRING

当我想要得到回应时。

我得到的Json回应如下:

[  
   {  
      "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"
   }
]

我的模特课:

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;
}

}

连接课程:

    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;
    }
}

和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();
            }
        });
    }
}

欢迎任何帮助或提示。

1 个答案:

答案 0 :(得分:-1)

尝试清理构建器build->清理有效的项目