这是我发出PATCH请求的函数:
public void patchWithParams2(Integer id, JSONObject params, final JsonCallback jsonCallback){
String URL = Constants.API_ENDPOINT + "/trips";
RestAdapter restAdapter = new RestAdapter.Builder()
.setClient(new OkClient()) //very important!!!
.setEndpoint(URL)//base url
.setRequestInterceptor(new RequestInterceptor() {//You may not need to pass headers, but I do, so this is how it's done.
@Override
public void intercept(RequestInterceptor.RequestFacade request) {
try{
String token = Utils.getEncodedAuthToken(PSLocationCenter.getInstance().pref);
request.addHeader("Authorization", "Basic " + token);
PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
request.addHeader("User-agent", "Passenger/" + pInfo.versionCode + " (" + System.getProperty("http.agent") + ")");
}catch (Exception e){
Log.e("","error trying to add user agent");
Utils.appendLog("ERROR trying to set agent:" + e.getMessage(),true);
}
request.addHeader("Content-Type", "application/json");
request.addHeader("Accept", "application/json");
}
})
.build();
PatientInfoAPI patchService = restAdapter.create(PatientInfoAPI.class);
TripUpdate trip = new TripUpdate();
trip.setNotes("#business");
patchService.update(trip, String.valueOf(id), new retrofit.Callback<TripUpdate>() {
@Override
public void success(TripUpdate userUpdates, retrofit.client.Response response) {
Log.i("All is good", "Nickname:" + userUpdates.toString());
Log.i("All is good", "Nickname:" + response.getBody().toString());
BufferedReader reader = null;
StringBuilder sb = new StringBuilder();
try {
reader = new BufferedReader(new InputStreamReader(response.getBody().in()));
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
Log.e("","error trying to get response" + e.getMessage());
e.printStackTrace();
}
} catch (IOException e) {
Log.e("","error trying to get response2" + e.getMessage());
e.printStackTrace();
}
String result = sb.toString();
Log.i("All is good", "Nickname2:" + result);
jsonCallback.onResponse(null, null);
}
@Override
public void failure(RetrofitError error) {
Log.i("All is good", "Nickname:" + error.getLocalizedMessage().toString());
jsonCallback.onResponse(null, null);
}
});
}
我得到了有效的回复,但我无法将其解析为JSON。 在此期间,我收到了以下错误:
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
Log.e("","error trying to get response" + e.getMessage());
e.printStackTrace();
}
错误:
04-20 17:43:57.877: E/(23529): error trying to get responseclosed
04-20 17:43:57.877: W/System.err(23529): java.io.IOException: closed
04-20 17:43:57.877: W/System.err(23529): at okio.RealBufferedSource$1.read(RealBufferedSource.java:367)
04-20 17:43:57.877: W/System.err(23529): at java.io.InputStreamReader.read(InputStreamReader.java:244)
04-20 17:43:57.877: W/System.err(23529): at java.io.BufferedReader.fillBuf(BufferedReader.java:130)
04-20 17:43:57.877: W/System.err(23529): at java.io.BufferedReader.readLine(BufferedReader.java:354)
04-20 17:43:57.877: W/System.err(23529): at nl.hgrams.passenger.services.PSTripService$17.success(PSTripService.java:1695)
04-20 17:43:57.877: W/System.err(23529): at nl.hgrams.passenger.services.PSTripService$17.success(PSTripService.java:1683)
04-20 17:43:57.877: W/System.err(23529): at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45)
04-20 17:43:57.877: W/System.err(23529): at android.os.Handler.handleCallback(Handler.java:725)
04-20 17:43:57.877: W/System.err(23529): at android.os.Handler.dispatchMessage(Handler.java:92)
04-20 17:43:57.877: W/System.err(23529): at android.os.Looper.loop(Looper.java:137)
04-20 17:43:57.877: W/System.err(23529): at android.app.ActivityThread.main(ActivityThread.java:5227)
04-20 17:43:57.877: W/System.err(23529): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 17:43:57.877: W/System.err(23529): at java.lang.reflect.Method.invoke(Method.java:511)
04-20 17:43:57.877: W/System.err(23529): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
04-20 17:43:57.877: W/System.err(23529): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
04-20 17:43:57.887: W/System.err(23529): at dalvik.system.NativeStart.main(Native Method)
这也是创建PATH和BODY的接口:
public interface PatientInfoAPI {
@PATCH("/{id}")
public void update(@Body TripUpdate updatedDAta, @Path("id") String id, Callback<TripUpdate> response);
}
是否可以将Body直接作为Json对象发送,而不是从Object发送?
答案 0 :(得分:2)
强制接口检索JsonElement:
@PATCH("/{id}")
public void update(@Body TripPatch updatedDAta, @Path("id") String id, Callback<JsonElement> response);
然后我就这样得到它:
String jsonObject = userUpdates.toString();