Ahsan Ali
Ahsan Ali

Reputation: 361

expected begin_array but was begin_object at line 1 column 2 path $ retrofit

I am working on a app in which i have to send a notification to a specific device but i am getting this error now i am searching for a solution
i am using rest api link https://fcm.googleapis.com/fcm/send i have tried alot and seeking a better solution.

//APi class Retrofit

public interface Api {

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

       })

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

  }

// Gson Class

// Score Class

//

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;

}

// DataScore

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

// Main Class

  @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();

        }
    });

}

Upvotes: 0

Views: 2830

Answers (1)

Elli White
Elli White

Reputation: 1570

expected begin_array but was begin_object at line 1 column 2 path $ retrofit - this error means that GSON was expecting an array to come back from your server. We know this because your return type in Retrofit is Call<List<Score>>. Because you are expecting a list, GSON attempts to decode your response as a JSON array. The error goes on to state that while expecting an array, the first json token it encountered was the begin_object token. Have you used a proxy or just inspected the raw payload in response.body? My guess is that your response has an enclosing json object around the array, or it is only sending back a single Score object.

Upvotes: 1

Related Questions