There is:
- Android app
- ASP.NET Web API
I create a Retrofit object:
Retrofit service = new Retrofit.Builder() .baseUrl("http://192.168.0.3/") // адрес WebApi .addConverterFactory(GsonConverterFactory.create()) .build(); In the code I call the API method I need, the data in the API comes correctly. As a test, I return the model that came to the controller to json
Action
[HttpPost] public JsonResult GetUser(UserModel model) { return Json(JsonConvert.SerializeObject(model)); } Model
public class UserModel { [JsonProperty("id")] public int Id { get; set; } [JsonProperty("email")] public string Email { get; set; } [JsonProperty("password")] public string Password { get; set; } [JsonProperty("name")] public string name { get; set; } [JsonProperty("device_id")] public string DeviceId { get; set; } [JsonProperty("register_date")] public string RegisterDate { get; set; } [JsonProperty("updated_date")] public string UpdatedDate { get; set; } } Through POSTMAN I get the correct json .
However, on Android I get Exception :
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224) at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37) at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25) at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:117) at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:211) at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:106) at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133) at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818) Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:213) ... 10 more Why doesn’t parse on Android the json came with the API?
UPD
API response:
{\"id\":1,\"email\":\"myemail@gmail.com\",\"password\":\"1234\",\"name\":null,\"device_id\":null,\"register_date\":null,\"updated_date\":null} In Android, in the callback handler, I’m dropping out here:
@Override public void onFailure(Call<UserModel> call, Throwable t) { t.printStackTrace(); }
{"id":0,"email":"myemail@gmail.com","password":"1234","name":null,"device_id":null,"register_date":null,"updated_date":null}- tCode