gpt4 book ai didi

java - retrofit转换为json字符串时出现Conversion Exception如何解决

转载 作者:行者123 更新时间:2023-11-30 10:42:29 26 4
gpt4 key购买 nike

我正在尝试向 api 发布一组参数,但出现了一个我不太明白的错误

java.lang.RuntimeException: retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

我怀疑可能导致此错误的三个方面

  1. 我要么错误地声明了最后一个参数

    @Query("AppTimes") 字符串应用时间

  2. 我的 Gson 转换结果不正确,也许我应该使用其他转换器

  3. 无法将 java 列表转换为 json

这是我的休息适配器的样子

public static DigitechService createService(Context context)
{
Gson gson = new GsonBuilder()
//.setDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'")
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss") // 2015-02-23T00:00:00
.create();

// Only allow LogLevel for Debug
RestAdapter restAdapter = new RestAdapter.Builder()
.setLogLevel(Globals.TEST == false ? RestAdapter.LogLevel.NONE : RestAdapter.LogLevel.FULL)
.setEndpoint(Globals.digitechApiBaseUrl)
.setConverter(new GsonConverter(gson))
.setRequestInterceptor(new AuthRequestInterceptor(context))
.build();

return restAdapter.create(DigitechService.class);
}

这是我的界面

@POST("/api/v1/LoadAppTimeUsage/")
void loadAppTimeUsage(
@Query("cellPhoneNumber") String cellPhoneNumber,
@Query("appName") String appName,
@Query("timeSpent") int timeSpent,
@Query("AppTimes") String appTimes,
Callback<List<ResultPost>> cb
);

我的发帖方式

public List<AppTimeUsage> postAppTimeUsage(List<AppTimeUsage> atu)
{
String json = new Gson().toJson(atu);
TinyDB settings = new TinyDB(mContext);
// Show progress
final ProgressDialog progress = new ProgressDialog(mContext);
progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {

}
});
progress.show();


String cellphoneNumber = settings.getString(AppPreferences.CONFIRMED_CellPhoneNUmber);


DigiTechClient.createService(mContext).loadAppTimeUsage(cellphoneNumber, appName ,timeSpent, json, new Callback<List<ResultPost>>() {
@Override
public void success(List<ResultPost> resultPosts, Response response) {
if (progress != null) progress.dismiss();
}

@Override
public void failure(RetrofitError error) {
if (progress != null) progress.dismiss();

DeviceApp.handleError(error);

if (error.getResponse() != null) {
RestError err = (RestError) error.getBodyAs(RestError.class);

Toast.makeText(mContext, err.Message, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(mContext, error.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
return atu;
}

我的 StackTrace

    java.lang.RuntimeException: retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
at com.xera.deviceinsight.structs.AppTimeUsageRecorder$2.failure(AppTimeUsageRecorder.java:163)
at retrofit.CallbackRunnable$2.run(CallbackRunnable.java:53)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
Caused by: retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
at retrofit.converter.GsonConverter.fromBody(GsonConverter.java:67)
at retrofit.RetrofitError.getBodyAs(RetrofitError.java:137)
at com.xera.deviceinsight.structs.AppTimeUsageRecorder$2.failure(AppTimeUsageRecorder.java:163)
at retrofit.CallbackRunnable$2.run(CallbackRunnable.java:53)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:200)
at com.google.gson.Gson.fromJson(Gson.java:810)
at com.google.gson.Gson.fromJson(Gson.java:775)
at retrofit.converter.GsonConverter.fromBody(GsonConverter.java:63)
at retrofit.RetrofitError.getBodyAs(RetrofitError.java:137)
at com.xera.deviceinsight.structs.AppTimeUsageRecorder$2.failure(AppTimeUsageRecorder.java:163)
at retrofit.CallbackRunnable$2.run(CallbackRunnable.java:53)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:387)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:189)
at com.google.gson.Gson.fromJson(Gson.java:810)
at com.google.gson.Gson.fromJson(Gson.java:775)
at retrofit.converter.GsonConverter.fromBody(GsonConverter.java:63)
at retrofit.RetrofitError.getBodyAs(RetrofitError.java:137)
at com.xera.deviceinsight.structs.AppTimeUsageRecorder$2.failure(AppTimeUsageRecorder.java:163)
at retrofit.CallbackRunnable$2.run(CallbackRunnable.java:53)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)

最佳答案

不要将参数转换为 JSON 字符串。相反,将列表对象作为正文参数发送,它将自动转换为 JSON。

举个例子:

@POST("/api/v1/LoadAppTimeUsage/")
void loadAppTimeUsage(
@Body List<MyApp> appTimes,
Callback<List<ResultPost>> cb
);

关于java - retrofit转换为json字符串时出现Conversion Exception如何解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38116196/

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