2017-08-02 121 views
1

我想通過一個arraylist到PHP做一些操作。當我測試與郵遞員我得到以下resoponse ..

{「correct_answers」:「2」,「total_question」:「5」,「消息」:「太棒了!你需要得分至少60%標記的移動一個新的水平。「」 message_percentage「:」 40" ,‘saved_message’:‘成功’}

enter image description here

但是,當我嘗試發送請求我收到此錯誤。 。

java.lang.IllegalStateException:預期的BEGIN_OBJECT,但是在第1行第1列處有STRING $

這是我的API接口

@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

請出示 「QuestionResult」 級。我認爲你犯了錯誤。 –

回答

1

問題是GSON期待的對象,但接收字符串,這是因爲JSON的格式可能是錯誤的。 JSON必須以

{ 

請檢查JSON格式。

編輯: 查看您的查詢的參數,我認爲你要發送的,而不是你把在郵遞員什麼服務器用一個String響應一個JSON而不是其他參數

+0

{「correct_answers」:「2」,「total_question」:「5」,「message」:「好極了,你需要至少60%的分數才能移動到下一關。」,「message_percentage」:「40 「,」saved_message「:」success「} 這是我用郵差測試時收到的格式。 –

+0

回波json_encode([ 「correct_answers」=>計數($ correct_answers)。 「」, 「total_question」=> $ total_question。 「」, 「消息」=> $結果[ 「消息」], 「message_percentage」 => $ percent。「」, 「message」=> $ results [「message」], 「saved_message」=> $ results [「saved_message」] ]); 這就是我在php代碼中定義的內容 –

+0

當您獲取數據時,嘗試複製並粘貼瀏覽器的結果,或者在reotrofit2中的「onFailure」中設置一箇中斷點以獲取答案。可能是你正在發送一個錯誤的參數,並不是相同的答案 –

0

我想你在QuestionResult類中犯了錯誤。它的格式應該是這樣

public class QuestionResult{ 

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

}

相關問題