我正在尝试将一组参数发布到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 $
我怀疑可能导致此错误的三个区域
我已声明最后一个参数错误
@Query(“AppTimes”)字符串appTimes
我的Gson转换结果不正确,也许我应该使用其他转换器
无法将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)
答案 0 :(得分:1)
不要将参数转换为JSON字符串。相反,将列表对象作为body参数发送,它将自动转换为JSON。
举个例子:
@POST("/api/v1/LoadAppTimeUsage/")
void loadAppTimeUsage(
@Body List<MyApp> appTimes,
Callback<List<ResultPost>> cb
);
答案 1 :(得分:0)
我认为这是你的gson转换器出现的问题。我的改装版本是不同的。我通过以下方式解决
.ssdl
希望它可以帮到你。