Android Retrofit Expected BEGIN_OBJECT, but was STRING in row 1 column 1 path $

I am using okhttp Retrofit in my Android application to make network requests. For one of the requests, I get this error:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: expected BEGIN_OBJECT, but there was STRING in row 1 column 1 path $

I see 201 response in the logs, but Retrofit throws this error. Below is my code.

signup(signupParams, new Callback<Member>() { @Override public void success(Member member, Response response) { if (member != null) { UserAccount userAccount = new UserAccount(member); userAccount.save(); } @Override public void failure(RetrofitError re) { BusProvider.post(new SignupFailedEvent(re, email)); } }); 

SignupParams value is

 {"emailAddress":" test@gmail.com ","password":"tester123","userSource":"APH"} 

I tested this json with jsonLint and it is valid json. And this is my member class, which should be the perfect answer.

 public class Member { public String emailAddress; public String token; public long id; public String firstName; public String lastName; } 

An example response should look something like this:

 { "emailAddress": " test@gmail.com ", "id": 1437811, "token": "sdhshdghsdhhsdbcjhbsjdhc", "firstName": "John", "lastName": "Smith" } 
+6
source share
2 answers

If the source code you posted for the Member class is accurate, you are not getting the JSON response that you think you are.

The error message means that the JSON parser found a string in which it expected a complex object.

Since you do not have complex objects in the Member class, the result is probably just not valid JSON (it does not start with an opening curly brace).

Try either enabling verbose logging in Retrofit, as suggested in one of the comments, or sending the same data to the API using a tool such as Postman and see what the result really is.

0
source

This is the reason. Your response is not JSON formatted. this may include string or expected}. to identify it. you need to check with the postman and change the type of presentation in the body as HTML. there you can see the full answer, and you can check with * https://jsonlint.com/ . even in the case of dynamic JSON. You can use JsonElement as an answer.

0
source

Source: https://habr.com/ru/post/1233007/


All Articles