com.google.gson.stream.MalformedJsonException:使用JsonReader.setLenient(true)在第1行第1列路径$接受格式错误的JSON

时间:2019-05-18 02:36:54

标签: android gson retrofit2

发送带有改型的请求时出现异常。尝试了各种解决方案,每次都给出格式错误的异常。

这是我的json响应

{
    "type": "admin",
    "user": "admin"
}

型号

public class LoginResponse {

@SerializedName("type")
@Expose
private String type;
@SerializedName("user")
@Expose
private String user;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getUser() {
return user;
}

public void setUser(String user) {
this.user = user;
}

}

ApiIntreface.java

@FormUrlEncoded
    @Headers("Content-Type: application/json" )
    @POST("login.php")
    Call<LoginResponse> login(
            @Field("user") String user,
            @Field("pass") String pass);

Api.java

public static ApiInterface getClient(){
    if (retrofit == null){
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        OkHttpClient client = new OkHttpClient();

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

MainActivity

Call<LoginResponse> call = Api.getClient().login(user,pass);
            call.enqueue(new Callback<LoginResponse>() {
                @Override
                public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
                    Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_LONG).show();
                }
                @Override
                public void onFailure(Call<LoginResponse> call, Throwable t) {
                    Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_LONG).show();
                }
            });

json格式可以。它仍然显示相同的异常。我正在使用改造2.5.0 谢谢您的任何答复。

0 个答案:

没有答案