3

I am trying to pass an arraylist to php to do some operations. when i tested with postman i got the following resoponse..

{"correct_answers":"2","total_question":"5","message":"Great!. You need to score at least 60% of marks to move next level..","message_percentage":"40","saved_message":"success"}

enter image description here

But when I try to send the request i am getting this error..

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

this is my api interface

@POST("questions.php")

@FormUrlEncoded

Call<QuestionResult> correctQuestionData(@Field("tag") String tag, @Field("str_question_answer[]") ArrayList<QuestionAnswers> str_question_answer,
                                         @Field("student_id")  String student_id, @Field("level") String level);
1
  • 1
    Please show "QuestionResult" class. I think you made mistake in it. Commented Aug 2, 2017 at 18:25

2 Answers 2

2

The problem is that Gson expecting an object, but is receiving a String, this is because the format of the JSON probably is wrong. The JSON has to start with

{

Please, review the JSON format.

EDIT: Review the parameters of your query, I think that you are sending others parameters instead of what you are putting in postman and the server responding with a String instead of a JSON

Sign up to request clarification or add additional context in comments.

3 Comments

{"correct_answers":"2","total_question":"5","message":"Great!. You need to score at least 60% of marks to move next level..","message_percentage":"40","saved_message":"success"} this is the format I received when I tested it with postman.
echo json_encode([ "correct_answers" => count($correct_answers)."", "total_question" => $total_question."", "message" => $results["message"], "message_percentage" => $percentage."", "message"=> $results["message"], "saved_message" =>$results["saved_message"] ]); This is what i have defined in the php code
Try to copy and paste the result from the browser when you fetch the data or put a break point into the funciont "onFailure" in reotrofit2 to kwnow the answer. May be you are sending a wrong paremeter and is not the same answer
0

I think you made mistake in QuestionResult class. It's format should be like

public class QuestionResult{

public String correct_answers;
public String total_question;
public String message;  
public String message_percentage;
public String saved_message;    

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.