我有一个接口,其定义方法如下:
@POST("/video/{id}")
public Float updateVideoRating(@Path(ID_PARAMETER) long id,
@Body Float rating);
现在每当我打电话给它时:
public float uploadVideoRating(long videoId, float rating) {
Log.d(TAG, "uploadVideoRating("
+ Long.toString(videoId)
+ ", "
+ Float.toString(rating)
+ "): operation called");
try {
return mVideoServiceProxy.updateVideoRating(videoId, rating);
} catch (Exception e) {
Log.e(TAG, "uploadVideoRating(): exception caught: " + e.toString());
e.printStackTrace();
return 0.0f;
}
}
...我有以下异常捕获(加上几行堆栈跟踪):
07-15 22:48:34.317 2131-2131/? D/VideoDataMediator﹕ uploadVideoRating(1, 3.0): operation called
07-15 22:48:34.320 2131-2131/? E/VideoDataMediator﹕ uploadVideoRating(): exception caught: retrofit.RetrofitError
07-15 22:48:34.320 2131-2131/? W/System.err﹕ retrofit.RetrofitError
07-15 22:48:34.321 2131-2131/? W/System.err﹕ at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:400)
07-15 22:48:34.321 2131-2131/? W/System.err﹕ at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
07-15 22:48:34.321 2131-2131/? W/System.err﹕ at java.lang.reflect.Proxy.invoke(Proxy.java:397)
07-15 22:48:34.321 2131-2131/? W/System.err﹕ at $Proxy0.updateVideoRating(Unknown Source)
07-15 22:48:34.321 2131-2131/? W/System.err﹕ at pl.dropby.vduc.VideoDataMediator.uploadVideoRating(VideoDataMediator.java:176)
我做错了什么?我怀疑附近的地方" @ Body"和" Float",但真的不知道......
答案 0 :(得分:0)
你应该在后台线程上进行网络调用:你有这个错误: android.os.NetworkOnMainThreadException
我建议您使用以下代码,以便通过在主线程(GUI线程)上进行网络调用来专注于客户端/服务器通信,然后通过asynctask或任何其他线程机制正确地执行操作:
// TODO:删除strictmode导入 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()。permitAll()。build();
// TODO:确保从VideoViewerActivity返回后列表视图正确更新 StrictMode.setThreadPolicy(策略);
答案 1 :(得分:0)
为什么不定义这样的方法:
@POST("/video/{id}/{rating}/")
public float updateVideoRating(@Path(ID_PARAMETER) long id, @Path(RATING_PARAMETER) float rating);
您不需要将评级放在体内。您可以通过路径访问它。
@RequestMapping(value=VideoSvcApi.VIDEO_DETAILS_PATH, method=RequestMethod.POST)
public @ResponseBody float updateVideoRating(@PathVariable(VideoSvcApi.ID_PARAMETER) long videoId,
@PathVariable(VideoSvcApi.RATING_PARAMETER) float rating,
HttpServletResponse response) throws IOException {
if (videos.containsKey(videoId)) {
return rating;
}
else {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return null;
}
}