MalformedJsonException using Retrofit 2

时间:2015-11-12 11:19:18

标签: android json retrofit

I am trying to get a JSON response from server in my Android app. I am using this:

OkHttpClient client = new OkHttpClient();
        client.interceptors().add(new Interceptor() {

            @Override
            public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
                com.squareup.okhttp.Response respuesta = chain.proceed(chain.request());
                Log.i("David", "Respuesta body: " + respuesta.body().string());
                return respuesta;
            }
        });
        Retrofit builder = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();

With this interface method:

@FormUrlEncoded
@POST ("login")
Call<JSONObject> login(@Header("x-imei") String imei, @Field("usuario") String user, @Field("password") String password, @Header("Accept") String accept, @Header("Content-Type") String content);

And this call:

Call<JSONObject> peticion=interfaz.login(imei, user, encriptedPass, "application/json", "application/json;charset=UTF-8");

This way, I am getting a MalformedJsonException in the Log:

[{"coderror":"-1","gravedad":"0","message":"com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 9","cause":"com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 9"}]

I am not able to see the JSON to check what is wrong with it.

The strange part is if I use curl this way:

curl -i -X POST -H "Accept: application/json" -H "Content-Type: application/json;charset=UTF-8" -H "x-id-cliente:demos" -H "x-imei:xxxxxxxxx" --data "{usuario: ‘user', password: ‘password'}" "https://my/server/url/api/login"

I get the JSON ok. What am I doing wrong? Why the curl is ok, but not the code call?

Thank you.

0 个答案:

没有答案