2017-02-18 73 views
0

我是Android開發新手。這是我的MainActivity.java。我已經設定的寬鬆,仍然收到錯誤D/onFailure: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $Retrift API的格式錯誤的JSON?

API的回報:它要麼Success或驗證像Error: FirstName is invalid.

private void saveUser() { 
    showpDialog(); 

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

    Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl("BASE_URL") 
      .addConverterFactory(GsonConverterFactory.create(gson)) 
      .build(); 

    APIService service = retrofit.create(APIService.class); 
    User user = new User(); 

    user.setFirstName(editName.getText().toString()); 

    Call<User> call = service.saveUser(user.getFirstName()); 

    call.enqueue(new Callback<User>() { 
     @Override 
     public void onResponse(Response<User> response) { 
      hidepDialog(); 
      Log.d("onResponse", "" + response.code() + 
        " response body " + response.body() + 
        " responseError " + response.errorBody() + " responseMessage " + 
        response.message()); 
     } 

     @Override 
     public void onFailure(Throwable t) { 
      hidepDialog(); 
      Log.d("onFailure", t.toString()); 
     } 
    }); 

} 

香港專業教育學院一直在尋找,他們都認爲我需要setLenient爲真,但我已經做了那。任何想法?

+0

你願意分享你的JSON嗎? –

+0

成功獲得response.body()嗎? 您對成功的確切迴應是什麼? '{}成功'? 因爲我對此的看法取決於它 –

+0

我使用PHP作爲我的API,並且返回只是「成功」或「驗證」的回聲 – Joseph

回答

1

SuccessValidation不是有效的JSON。我認爲這就是爲什麼它是畸形的。 JSON是這樣的:

{ "status": "success" } 

所以,你不應該把它解析爲JSON,而只是作爲一個原始字符串來讀取。或者,最好讓服務器的響應成爲像上面這樣有效的JSON。

使它成爲RESTful的更好,它從HTTP響應狀態而不是響應主體讀取狀態,或者是200 OK或其他狀態碼。但它沒有問題,所以現在只需使用JSON。

+0

你是對的,我只需要編輯我對有效JSON的迴應。謝謝! – Joseph