简体   繁体   English

使用Retrofit2解析字符串数组

[英]Parsing an array of strings using Retrofit2 for android

I am relatively new to retrofit and I have an issue in parsing some string arrays which are a part of a JSON response. 我是相对较新的改装者,在解析一些字符串数组(这是JSON响应的一部分)时遇到问题。

This is the JSON response. 这是JSON响应。

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

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

How do I approach this? 我该如何处理?

Thanks in advance :) 提前致谢 :)

You need to create POJO class like below 您需要创建如下的POJO类

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. 运行良好。

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. 通过查看您的评论,我认为您的POJO类是正确的,但是将其与Retrofit调用一起使用的方式是错误的。 Make sure that the POJO class is like below, 确保POJO类如下所示,

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. 并使用上面的POJO类。

Your API interface 您的API介面

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 下次使用API​​接口

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). 请注意,这里我使用Foo POJO类作为参数而不是List<Strain> (就像您的代码中使用的那样)作为Call参数。 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 java.lang.IllegalStateException:应为BEGIN_ARRAY,但在第1行第2列路径$错误处为BEGIN_OBJECT

For demonstration purpose I used Foo example. 为了演示,我使用了Foo示例。 Change it according to your requirements. 根据您的要求进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM