2017-06-16 19 views
0

我在異步任務中有此調用。所有參數都是正確的。在postman或advance rest client中,調用工作正常,並返回一個包含對象列表的json。但是,如果我嘗試在Android的做這個調用與春天,我有這樣的錯誤:安卓春季JsonMappingException httprequest

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of java.lang.String out of START_OBJECT token at [Source: [email protected]; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token at [Source: [email protected]; line: 1, column: 1]

這是預期的JSON響應

[ 
{ 
    "id": 57, 
    "user_id": 2, 
    "category_id": 5, 
    "notification_title": "test 9", 
    "notification_body": "breve 9", 
    "description": "Sd sdfds sdfs dfsd fdsf", 
    "image": null, 
    "code": "2-5942525969dfa", 
    "start_date": null, 
    "expiration_date": null, 
    "created_at": "2017-06-15 09:24:41", 
    "updated_at": "2017-06-15 09:24:41" 
}, 
{ 
    "id": 56, 
    "user_id": 2, 
    "category_id": 4, 
    "notification_title": "test 8", 
    "notification_body": "breve desc 8", 
    "description": "Sdfsdf sdfds dsfs dsfds", 
    "image": null, 
    "code": "2-59424e24570a2", 
    "start_date": null, 
    "expiration_date": null, 
    "created_at": "2017-06-15 09:06:44", 
    "updated_at": "2017-06-15 09:06:44" 
}] 

這是我的AsyncTask

new AsyncTask<Void, Void, String>() { 
     @Override 
     protected String doInBackground(Void... params) { 

      JSONObject body = new JSONObject(); 

      try { 
       body.put("api_token", Config.API_TOKEN); 
       body.put("user_id", Config.USER_ID); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      try { 
       return template.postForObject(
         Config.SERVICE_URL_BASE + "/getallposts", 
         body.toString(), 
         String.class 
       ); 
      } catch (Exception e) { 
       listener.onError(e.getMessage()); 
      } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(String response) { 
      if (response != null) { 
       try { 
        List<JSONObject> jsonObjects = ResponseConverter.toJSONArray(response); 
        listener.onSuccess(jsonObjects); 
       } catch (JSONException e) { 
        listener.onError(context.getResources().getString(R.string.cannot_load_posts)); 
       } 
      } else { 
       listener.onError(context.getResources().getString(R.string.cannot_load_posts)); 
      } 
     } 

    }.execute(); 

可有人請幫幫我?

回答

0

問題解決了,Spring框架其餘tempolate版本是錯誤的,我用的是1.0.1版本,但正確的versione是:

compile 'org.springframework.android:spring-android-rest-template:2.0.0.M3'