为什么我得到jsonsyntaxexception错误

8gsdolmq  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(312)

我是新来的 retrofit (我用的是 volley 在此之前,我在改装方面做得很好,直到出现以下错误:-
com.google.gson.jsonsyntaxexception:java.lang.illegalstateexception:应为int,但在第8行第37列路径$.response.data.book\服务\ id处为布尔值
我尝试了这个网站提供的每一个解决方案,但无法控制自己,因为我是新手 retrofit . 我想是因为 JSON parsing 错误。我不知道怎么处理。这可能是一个重复的问题,但请帮助。下面是我的代码:
请求和获取数据:

private void makeBookingRequest(String position) {
    final CustomProgressDialog dialog = new CustomProgressDialog();
    dialog.show(getSupportFragmentManager(),"tag");
    SharedPreferences preferences = getSharedPreferences("MYSharedPref",MODE_PRIVATE);
    String sessionkey = preferences.getString("sessionkey",null);
    System.out.println(sessionkey);
    String serviceId = position;
    System.out.println(position);
    APIEndPoints endPoints = Url.getInstance().create(APIEndPoints.class);
    Call<Book> call = endPoints.makeBookingRequest(serviceId,sessionkey);
    call.enqueue(new Callback<Book>() {
        @Override
        public void onResponse(Call<Book> call, retrofit2.Response<Book> response) {
            dialog.dismiss();
            if (!response.isSuccessful()) {
                Toast.makeText(HomeActivity.this, "server is not responding", Toast.LENGTH_SHORT).show();
            }
            else if(response.body() != null){
                Book bookData = response.body();
                String message = bookData.response.message;
                Toast.makeText(HomeActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<Book> call, Throwable t) {
            dialog.dismiss();
            Toast.makeText(HomeActivity.this, "Error" + t.getLocalizedMessage(), 
Toast.LENGTH_SHORT).show();
            System.out.println(t);
        }
    });
}

型号等级:

package com.medpal.medpal_client.Models;
    import com.google.gson.annotations.SerializedName;

    public class Book {
    @SerializedName("response")
    public ResponseEntity response;

    public class ResponseEntity{
        @SerializedName("data")
        public DataEntity data;
        @SerializedName("secondary_message")
        public String secondary_message;
        @SerializedName("message")
        public String message;
        @SerializedName("code")
        public int code;
    }
    public class DataEntity {
        @SerializedName("book_service_id")
        public int book_service_id;
    }
}

API端点

@FormUrlEncoded
@Headers({"apikey: testapikey", "Content-Type:application/x-www-form-urlencoded" })
@POST("service/accept?")
Call<Book> makeBookingRequest(
  @Field("service_id") String ServiceId,
  @Field("session_key") String sessionKey);

url类

public class Url {
    public static final String base_url = "http://www.medpal.net/api/v1/";
    public static final String serviceUrl = "http://www.medpal.net/api/v1/services?";
        public static Retrofit retrofit;
        public static Retrofit getInstance() {
            if (retrofit == null) {
                retrofit = new Retrofit.Builder()
                        .baseUrl(base_url)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
            }
            return retrofit;
        }

}

来自服务器的最终响应:

{
    "response": {
        "error": [],
        "code": 200,
        "message": "Service booked",
        "secondary_message": "Service booked successfully",
        "data": {
            "book_service_id": 35
        }
    }
}
j8ag8udp

j8ag8udp1#

要准确地检测问题,您需要使用拦截器来记录服务器响应,要记录服务器响应,您可以使用okhttp3,这里就是一个例子。

private OkHttpClient provideOkhttpClient() {
        OkHttpClient.Builder client = new OkHttpClient.Builder();
        client.readTimeout(15000, TimeUnit.MILLISECONDS);
        client.writeTimeout(70000, TimeUnit.MILLISECONDS);
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        client.addInterceptor(interceptor);
        return client.build();
    }

并将此添加到您的reformation.builder

.client(provideOkhttpClient())

这些是给grad尔的

implementation 'com.squareup.okhttp3:okhttp:3.12.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.5.0'

如果你这样做,你会看到你的一切发送和从服务器在你的日志猫。

相关问题