Retrofit2.0 gets MalformedJsonException while the json seems correct?

前端 未结 3 1114
北海茫月
北海茫月 2020-11-29 08:12

I am using retrofit:2.0.0-beta4 for my android app.

I tried to add a user with Retrofit, the user is correctly created in Database, however I got the following error

相关标签:
3条回答
  • 2020-11-29 08:34

    Finally I solved my problem which is not related to the json lenient mode, something wrong with my POST response (there some other non json output before the json data).

    Here is the response from JakeWharton regarding how to set Gson lenient mode:

    make sure that you have:compile 'com.google.code.gson:gson:2.6.1'

    Gson gson = new GsonBuilder()
            .setLenient()
            .create();
    
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(client)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();
    
    0 讨论(0)
  • 2020-11-29 08:43

    I solved the problem

    Gson gson = new GsonBuilder().setLenient().create();
    
    OkHttpClient client = new OkHttpClient();
    
    Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://kafe.netai.net/")
        .client(client)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build();
    
    
    
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.google.code.gson:gson:2.7'
    
    0 讨论(0)
  • 2020-11-29 08:56

    I solved my problem by removing extra HTML text and other white spaces from my JSON file.

    0 讨论(0)
提交回复
热议问题