gpt4 book ai didi

java - 为什么我收到 JsonSyntaxException 错误

转载 作者:行者123 更新时间:2023-12-02 09:04:54 27 4
gpt4 key购买 nike

我是retrofit的新手(我之前使用volley),在此之前我在retrofit方面做得很好,直到出现此错误:-

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected int but was Boolean at line 8 column 37 path $.response.data.book_service_id

我尝试了本网站提供的所有解决方案,但无法帮助自己,因为我是retrofit的新手。我认为这是因为 JSON 解析 错误。我不知道该如何处理。这可能是一个重复的问题,但请帮忙。下面是我的代码:

请求和获取数据:

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
}
}
}

最佳答案

要准确检测问题,您需要使用拦截器来记录服务器响应,要记录服务器响应,您可以使用 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();
}

并将其添加到您的 Retrofit.Builder

.client(provideOkhttpClient())

这些是针对 Gradle 的

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

如果您这样做,您将在 Logcat 上看到您从服务器发送和获取的所有内容。

关于java - 为什么我收到 JsonSyntaxException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59896907/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com