0

I have this call in async task. All parameters are correct. In postman or advance rest client the call work fine and It return a json with a list of objects. But if I try to do this call in android with spring I have this error:

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of java.lang.String out of START_OBJECT token at [Source: org.apache.http.conn.EofSensorInputStream@1961b5e; 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: org.apache.http.conn.EofSensorInputStream@1961b5e; line: 1, column: 1]

This is the expected json response

[
{
    "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"
}]

this is my 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();

Can someone help me please?

1 Answer 1

0

Problem solved, the spring framework rest tempolate version was wrong, I used the 1.0.1 RELEASE but the correct versione is:

    compile 'org.springframework.android:spring-android-rest-template:2.0.0.M3'
Sign up to request clarification or add additional context in comments.

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.