【问题标题】:expected begin_array but was begin_object at line 1 column 2 path $ retrofit预期 begin_array 但在第 1 行第 2 列路径 $ 改造处是 begin_object
【发布时间】:2019-06-29 20:47:41
【问题描述】:

我正在开发一个应用程序,在该应用程序中我必须向特定设备发送通知,但现在我正在寻找解决方案
我正在使用rest api链接https://fcm.googleapis.com/fcm/send 我已经尝试了很多并寻求更好的解决方案。

//API 类改造

public interface Api {

   @Headers({"Content-Type:application/json",
       "Authorization:key=SECRET_KEY"

       })

    @POST("fcm/send")
      Call<List<Score>> getValues(@Body Score score);

  }

// Gson类

// 分数等级

//

public class Score {

public DataScore getData() {
    return data;
}

public void setData(DataScore data) {
    this.data = data;
}

public String getTo() {
    return to;
}

public void setTo(String to) {
    this.to = to;
}

@SerializedName("data")
@Expose
private DataScore data;
@SerializedName("to")
@Expose
private String to;

}

// 数据评分

  class DataScore {

@SerializedName("score")
@Expose
private String score;

public String getScore() {
    return score;
}

public void setScore(String score) {
    this.score = score;
}

public String getTime() {
    return time;
}

public void setTime(String time) {
    this.time = time;
}

@SerializedName("time")
@Expose
private String time;
    }

// 主类

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

    String token =FirebaseInstanceId.getInstance().getToken();
    String key=token;
    DataScore dataScore =new DataScore();
    dataScore.setScore("aaa");
    dataScore.setTime("aa");

    Score score=new Score();
    score.setData(dataScore);
    score.setTo(token);



    Retrofit retrofit =new Retrofit.Builder().baseUrl("https://fcm.googleapis.com/").addConverterFactory(GsonConverterFactory.create()).build();

    Api api= retrofit.create(Api.class);

    api.getValues(score).enqueue(new Callback<List<Score>>() {
        @Override
        public void onResponse(Call<List<Score>> call, Response<List<Score>> response) {
            if (response.isSuccessful()){

                Toast.makeText(MainActivity.this, response.body().toString() +"Successfull", Toast.LENGTH_SHORT).show();

            }

            else
            {
                Toast.makeText(MainActivity.this, response.message()+"FAiled",Toast.LENGTH_SHORT).show();

            }
        }
        @Override
        public void onFailure(Call<List<Score>> call, Throwable t) {
            Toast.makeText(MainActivity.this, t.getMessage() +"Failure", Toast.LENGTH_SHORT).show();

        }
    });

}

【问题讨论】:

    标签: java android json


    【解决方案1】:

    expected begin_array but was begin_object at line 1 column 2 path $ retrofit - 此错误意味着 GSON 期望从您的服务器返回一个阵列。我们知道这一点,因为您在 Retrofit 中的返回类型是 Call&lt;List&lt;Score&gt;&gt;。因为您需要一个列表,所以 GSON 会尝试将您的响应解码为 JSON 数组。错误继续指出,虽然期待一个数组,但它遇到的第一个 json 令牌是 begin_object 令牌。您是否使用了代理或只是检查了response.body 中的原始有效负载?我的猜测是您的响应在数组周围有一个封闭的 json 对象,或者它只发回一个 Score 对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-08
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多