1

If I send id, password to the server it has to give me a json but It gives me an error.

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path

This is Retrofit Code:

public void postCheckUser(String id, String password) {
    retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(url)
            .build();

    Map map = new HashMap();
    map.put("id",id);
    map.put("password", password);

    webService = retrofit.create(WebService.class);
    Call<PastMemo> call = webService.getPastMain(map);
    call.enqueue(new Callback<PastMemo>() {
        @Override
        public void onResponse(Call<PastMemo> call, Response<PastMemo> response) {
            if (response.code() == 200) {
                title = response.body().getTitle();
                letter= response.body().getLetter();
                date = response.body().getDate();
                Toast.makeText(getActivity(), "SignIn Success", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getActivity(), "SignIn Failed", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<PastMemo> call, Throwable t) {
            Toast.makeText(getActivity(), "SignIn Denied", Toast.LENGTH_SHORT).show();
        }
    });
}

This is the WebService Code:

public interface WebService {

    @FormUrlEncoded
    @POST("PastMain")
    Call<PastMemo> getPastMain(
            @FieldMap Map<String, String> user
    );
}

This is PastMemo code

public class PastMemo {

@SerializedName("title")
@Expose
private String title;
@SerializedName("letter")
@Expose
private String letter;
@SerializedName("date")
@Expose
private String date;

public String getTitle() {
    return title;
}

public String getLetter() {
    return letter;
}

public String getDate() {
    return date;
}
}

EDIT: This the json which I have to get

[
    {
        "title": null,
        "letter": null,
        "date": null
    },
    {
        "title": "a",
        "letter": "b",
        "date": "c"
    }
]
dogyhbin2
  • 25
  • 6
  • Please post that line at which it is throwing the same exception –  Aug 27 '17 at 06:31
  • I debugged all the lines to check the error line but It doesn't show up. It only goes to the onFailure() method. – dogyhbin2 Aug 27 '17 at 06:45

1 Answers1

0

The exception : com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path.

occurs while deserialising, Gson was expecting a JSON object, but found a JSON array. Since it couldn't convert from one to the other, it threw this exception. Refer this : GSON throwing "Expected BEGIN_OBJECT but was BEGIN_ARRAY"?

Try this:

 @FormUrlEncoded
    @POST("PastMain")
    Call<List<PastMemo>> getPastMain(
            @FieldMap Map<String, String> user
    );

Retrofit Code:

Call<List<PastMemo>> call = webService.getPastMain(map);
call.enqueue(new Callback<List<PastMemo>>() {
    @Override
    public void onResponse(Call<List<PastMemo>> call, Response<List<PastMemo>> response) {

     List<PastMemo> ls = response.body();
     //your implementation here
    }

    @Override
    public void onFailure(Call<PastMemo> call, Throwable t) {
        Toast.makeText(getActivity(), "SignIn Denied", Toast.LENGTH_SHORT).show();
    }
});
Chanchal Roshan
  • 164
  • 1
  • 3
  • 13