Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path

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("[email protected]","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.

like image 570
abrutsze Avatar asked Mar 16 '18 07:03

abrutsze


2 Answers

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

// and in you adapter set this instance
GsonConverterFactory.create(gson)
like image 98
Florescu Cătălin Avatar answered Sep 19 '22 10:09

Florescu Cătălin


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" }

like image 44
Mars Avatar answered Sep 18 '22 10:09

Mars