0

I was doing every post method of my app using this method:

1-This is the interface where I do my get and post methods:

@POST("feedback/challenge/newRating")
Call<IncreasedChallengeFeedback> challengeFeedbackIncreaseRating(@Body IncreasedChallengeFeedback increasedFeedbackChallenge);

2- the method to do the call

public void increaseRatingCFb(IncreasedChallengeFeedback increasedChallengeFeedback) {

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

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://10.0.2.2:3000/")
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();

    api = retrofit.create(INodeJS.class);

    Call<IncreasedChallengeFeedback> call = api.challengeFeedbackIncreaseRating(increasedChallengeFeedback);
    call.enqueue(new Callback<IncreasedChallengeFeedback>() {
        @Override
        public void onResponse(Call<IncreasedChallengeFeedback> call, Response<IncreasedChallengeFeedback> response) {
            if (!response.isSuccessful()) {
                System.out.println("!= Successful" + response.code());
                return;
            }
        }

        @Override
        public void onFailure(Call<IncreasedChallengeFeedback> call, Throwable t) {
            System.out.println("OnFailure: " + t.getMessage());
        }
    });
}

3- Here I just create an object of the class to use with the method

holder.likeButton.setOnLikeListener(new OnLikeListener() {
        @Override
        public void liked(LikeButton likeButton) {

         IncreasedChallengeFeedback icfb = new IncreasedChallengeFeedback("8","2");
         increaseRatingCFb(icfb);

        }

4- And finally here its the class

public class IncreasedChallengeFeedback {


private String cFeedbackRating;
private String cFeedbackID;

public IncreasedChallengeFeedback(String cFeedbackRating, String cFeedbackID) {
    this.cFeedbackRating = cFeedbackRating;
    this.cFeedbackID = cFeedbackID;
}

}

And with this I am getting this errors:

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

So I added this:

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

And then the error changes to this:

Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

The thing is that I did all my other posts and get methods using that method and then suddenly I'm stuck with this errors.

1 Answers1

0

Your JSON object is most likely malformed. Print the JSON object you receive to see if it's malformed in any way. The same question was asked here, so check for more info: https://stackoverflow.com/a/28418787/13709773

chef417
  • 366
  • 5
  • How can I do it? I usually just create an object and it gets converted to JSON but for some reason no its giving me that error. I did the exactly same procedure for every other post method – Tomás Santos Jun 09 '20 at 01:20
  • You can create class like this: https://gist.github.com/voghDev/0bd7050a675ee0f31e51 and add it to the RetrofitBuilder as explained in the first comment. Then the raw JSON will appear in your logs when you make the call. – chef417 Jun 09 '20 at 01:26
  • After doing that I get this : "ATAL EXCEPTION: OkHttp Dispatcher" and "java.lang.ClassCastException: java.lang.String cannot be cast to org.json.JSONArray" – Tomás Santos Jun 09 '20 at 11:11
  • If using okhttp 3+, see here: https://gist.github.com/voghDev/0bd7050a675ee0f31e51#gistcomment-1750623 – chef417 Jun 09 '20 at 21:03