6

Trying to send info in JSON format using Retrofit, but it enters into Retrofit's onFailure method and throws the following error:

com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

So far I have tried to solve it by using the answers from the following links: 1) MalformedJsonException with Retrofit API? 2) Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

Here is my code:

Retrofit interface:

public interface ServerApi {
    @POST("/register/test")
    Call<User> createAccount(@Body User user);
}

MainActivity with connection stuff:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        User user= new User("myemail@mail.ru","vercode100");
        sendNetworkRequest(user);
    }

    private void sendNetworkRequest(User user){

        //create Retrofit instance
        Retrofit.Builder builder= new Retrofit.Builder()
                .baseUrl("http://localhost:8080")
                .addConverterFactory(GsonConverterFactory.create());

        Retrofit retrofit= builder.build();

        //Get client and call object for the request
        ServerApi client= retrofit.create(ServerApi.class);
        Call<User> call= client.createAccount(user);

        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
            }
        });

    }
}

User class:

public class User {
    private String email;
    private String verificationCode;

    public User(String email, String verificationCode) {
        this.email = email;
        this.verificationCode = verificationCode;
    }

}

The server side expects JSON like this:

{
    "email" : "user.email",
    "verificationCode" : "123456"
}

I know that there are some common questions in stackoverflow, but neither of them fully solves my problem.

abrutsze
  • 446
  • 1
  • 6
  • 20
  • Possible duplicate of [Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $](https://stackoverflow.com/questions/39918814/use-jsonreader-setlenienttrue-to-accept-malformed-json-at-line-1-column-1-path) – Erfan Jan 01 '19 at 06:03

5 Answers5

3
GsonBuilder gsonBuilder = new GsonBuilder();  
gsonBuilder.setLenient();  
Gson gson = gsonBuilder.create(); 

// and in you adapter set this instance
GsonConverterFactory.create(gson)
Florescu Cătălin
  • 4,320
  • 1
  • 20
  • 32
1

The exception is not thrown when sending your data, but it's thrown when gson tries to parse the server response. According to your code you are expecting the server to respond with a User object but you're also sending a User object.

Does your server respond with the following? Because that's what you're telling retrofit with Call<User> createAccount(@Body User user)

{ "email" : "user.email", "verificationCode" : "123456" }

Mars
  • 3,842
  • 10
  • 37
  • 59
  • The server side gets from me an empty query, without any JSON :( – abrutsze Mar 16 '18 at 11:35
  • @abrutsze does it respond with anything? The empty query can be cause by a lot of different things. Can you change "Call" into "Call" and see if you still get the exception? – Mars Mar 16 '18 at 11:56
  • 1
    Connection status is 200, but the body that I send is empty – abrutsze Mar 16 '18 at 14:00
  • That means gson is unable to parse an empty response into a User object like you want it to by defining Call in your interface – Mars Mar 17 '18 at 23:41
  • 1
    I think it is creating the JSON file in a wrong way ( in my side, before the sending process ) – abrutsze Mar 20 '18 at 11:42
0

Make sure that your header are correct.

In my case I send Accept-Encoding: gzip and so the server return the value with gzip. So I remove it and it works well now.

Hope helpfull...

Obsidian
  • 2,760
  • 6
  • 14
  • 24
0

Remove the Accept-Encoding header. OkHttp will take care of gzip for you, but only if you aren't setting up gzip yourself.

moh.sh
  • 1
  • 1
0

may be in your metadata you have to properly write the JSON file. for example if it is array or two objects make sure not to miss the square bracket.

[
    { 
       "email" : "user.email", "verification" : "123456" 
    },
    { 
       "email" : "user.email", "verification" : "123456" 
    }
]
sudo97
  • 910
  • 1
  • 7
  • 20
  • please fix the formatting on your code, your last square bracket needs an additional space preceding it – KBriggs Mar 02 '20 at 17:46