0

I already checked the other related question but none of what they suggested works for my case. I am working with Retrofit 2.2 and GSON to get the Rest response : this is what the response look like:

   {"idUser":0,"nom":"kaddour","prenom":"hanedi","login":"hanedi@gmail.com","password":null,"genre":"f","adresse":"ettahrir","codePostal":8050,"tel":20333473,"active":0,"panneaux":null,"lesalertes":null,"paysuser":null,"ville":null}

Its a valid json format yet whenever I call using this code I get the Expected Begin_Object exception:

         apiService = RestService.createService(SolarAPIService.class);
    Call<User> call = apiService.authentif(email, pass);
    call.enqueue(new Callback<User>() {
        @Override
        public void onResponse(Call<User> call, retrofit2.Response<User> response) {
            user=response.body();}

I cant find where things went wrong. Any help appreciated ! Edit this is my User class :

        public class User {
@SerializedName("idUser")
@Expose
private int idUser;
@SerializedName("nom")
@Expose
private String nom;
@SerializedName("prenom")
@Expose
private String prenom;
@SerializedName("login")
@Expose
private String login;
@SerializedName("password")
@Expose
private String password;
@SerializedName("genre")
@Expose
private String genre;
@SerializedName("adresse")
@Expose
private String adresse;
@SerializedName("codePostal")
@Expose
private int codePostal;
@SerializedName("tel")
@Expose
private int tel;
@SerializedName("active")
@Expose
private int active;
@SerializedName("panneaux")
@Expose
private List<PanneauV> panneaux;
@SerializedName("lesalertes")
@Expose
private List<Alerte> lesalertes;
@SerializedName("paysuser")
@Expose
private Pays paysuser;
@SerializedName("ville")
@Expose
private Ville ville;
     //Constructor with arguments and Constructor without arguments
     //getters and setters
Community
  • 1
  • 1
MeknessiHamida
  • 185
  • 1
  • 8
  • 28
  • Please show us your User class – yozzy Mar 22 '17 at 09:37
  • I believe the JSON you provided is **not** complete: Gson is reporting that `{`, an object begin, is expected, but the response has a string. Check your **real** JSON and make sure that both `$.paysuser` and `$.ville` are not strings. Or, maybe, your whole response is a string. – Lyubomyr Shaydariv Mar 22 '17 at 10:19
  • this my call ` Call authentif(@Path("login") String login,@Path("pwd")String pwd);` it clearly should return a User Object and both `paysuser` and `ville` are objects. Could it be maybe caused by the `password` wihich a String returned as null – MeknessiHamida Mar 22 '17 at 10:23
  • @MeknessiHamida No, `null` is always safe and cannot cause the `BEGIN_OBJECT` issue by definition. Your `User` mapping defines object (`{`) **only** in three cases: itself, its `paysuser` and its `ville` properties. There no any other object mappings. Please check the response. To identify the exact location, read the exception message: it must contain the failed location path and, if I'm not wrong, line and column position, so you can use that coordinate to identify the "broken" token. – Lyubomyr Shaydariv Mar 22 '17 at 10:45
  • `Expected BEGIN_OBJECT but was STRING at line 1 column 1 path` this my exception all the nulls returned are objects except for password Itried the server response on RestClient (the firefox addon) the response looks perfectly valid. It shouldnt be read as a String – MeknessiHamida Mar 22 '17 at 10:56
  • @MeknessiHamida Such a message must be posted to the question when it's posted. Anyway, make sure that your response is not wrapped into a string whose content is your JSON. – Lyubomyr Shaydariv Mar 22 '17 at 11:51
  • How to make sure that my response is not wrapped into a string ? – MeknessiHamida Mar 22 '17 at 12:58
  • @MeknessiHamida There are maaany options. If you can obtain the URL yourself, you can use even your web-browser and read the response source, or use utilities like `curl`. If both are not easy to use, you can decorate Retrofit 2 response to log the input stream behind it, or try something like this: http://stackoverflow.com/questions/21886313/how-to-log-request-and-response-body-with-retrofit-android – Lyubomyr Shaydariv Mar 22 '17 at 13:37
  • I tried running it on my web-browser and tried with postman. that's the only response am getting. – MeknessiHamida Mar 22 '17 at 14:06
  • @MeknessiHamida For the experiment's sake, make your `authentif` to accept `String` rather than `User`, just to make sure if it's really a string. No need to parse it manually, of course: just check if your callback can accept the string response without issue. It has to. – Lyubomyr Shaydariv Mar 22 '17 at 15:01
  • Actually I have another json response that returns a String and another one that returns a `List` and it works just fine, this is the only response that creates such an issue. – MeknessiHamida Mar 22 '17 at 18:36
  • @LyubomyrShaydariv I tried returning `String` and the response is `User [my data]` – MeknessiHamida Mar 22 '17 at 20:22

1 Answers1

0

Your User class has to implement Serializable.

You don't need to override your attribute with SerializeName if the name is the same. I think you can remove @expose too.

yozzy
  • 344
  • 2
  • 15