JSON Error "java.lang.IllegalStateException: expected BEGIN_OBJECT, but there was STRING in row 1 column 1 path $"

public interface UserService { @POST(Constants.Api.URL_REGISTRATION) @FormUrlEncoded BaseWrapper registerUser(@Field("first_name") String firstname, @Field("last_name") String lastname, @Field("regNumber") String phone, @Field("regRole") int role); public BaseWrapper registerUser(User user) { return getUserService().registerUser(user.getFirstName(), user.getLastName(), user.getPhone(), user.getRole()); } 

This throws an exception.

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

Many thanks for the help.

+6
source share
3 answers

Look at the error you received.

Expected BEGIN_OBJECT

Your JSON is an object, and all JSON objects are enclosed in braces ({}). BEGIN_OBJECT therefore {. And he is waiting for him somewhere.

but was STRING

But instead, he found the line "Something." Still not telling us where.

row 1 column 1 path $

Oh, great. Line 1 of column 1. Which is the beginning of JSON. So, you forgot to put it all in {} (or at least you forgot the first, but I'm sure you forgot both of them).

+21
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/ .

0
source

Clean and rebuild the project. This is because there is confusion in the Gson file.

-3
source

All Articles