我在使用改造2进行POST请求时遇到了麻烦。我正在尝试使用Retrofit的GsonConverterFactory将POJO对象转换为JSON。
我有GET请求使用GsonConverterFactory将主体转换为POJO类,但在POST上它似乎不起作用(POJO到JSON)。我在Android Studio中使用它并使用Gradle来构建我的项目。这些是我的Gradle依赖项:
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.okhttp:okhttp:2.7.5'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
这是我的Api界面代码:
public interface EhabrApi {
@POST("/create-account")
Call<ResponseBody> createUser(@Body User user);
}
这是发出请求的代码:
User newUser = new User("Bob", "Smith", "bob@gmail.com", "password", "Seattle", "1990-01-01");
System.out.println("User is " + new Gson().toJson(newUser));
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
EhabrApi ehabrApi = retrofit.create(EhabrApi.class);
Call<ResponseBody> call = ehabrApi.createUser(newUser);
System.out.println("Request is " + new Gson().toJson(call.request()));
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
int statusCode = response.code();
System.out.println("Recieved " + statusCode);
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
System.out.println("Failed call " + t);
System.out.println("stack trace: ");
t.printStackTrace();
}
}
);
这是我的输出和堆栈跟踪:
Creating new account
User is {"Birthday":"1990-01-01","City":"Seattle","Email":"bob@gmail.com","FirstName":"Bob","LastName":"Smith","Password":"password"}
Request is {"headers":{"namesAndValues":[]},"method":"POST","url":{"host":"mywebpage.com","password":"","pathSegments":["create-account"],"port":8080,"scheme":"http","url":"http://mywebpage.com","username":""}}
Failed call com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $
stack trace:
com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $
at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1567)
at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1416)
at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:597)
at com.google.gson.stream.JsonReader.peek(JsonReader.java:429)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:201)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:116)
at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:211)
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:106)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
修改 现在我按照建议放入拦截器。 POST请求看起来对我来说......这是:
--> POST http://[mywebpageurl.com]/create-account http/1.1
Content-Type: application/json; charset=UTF-8
Content-Length: 125
{"Birthday":"1990-01-01","City":"Seattle","Email":"bob@gmail.com","FirstName":"Bob","LastName":"Smith","Password":"password"}
--> END POST (125-byte body)
<-- 200 OK http://[mywebpageurl.com]/create-account (100ms)
答案 0 :(得分:0)
你的logcat消息中很清楚
Failed call com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $
因此,这意味着您等待JSONObject
但是由于您的请求,您将获得格式错误的JSONObject。要解决问题,请检查您的JSON响应。