Android + Retrofit - POST 方法抛出...一些未指定的东西
Android + Retrofit - POST method throws... some unspecified thing
我有一个接口,其方法定义如下:
@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" 附近的某处,但真的不知道是什么...
你应该在后台线程上进行网络调用:你有这个错误:
android.os.NetworkOnMainThreadException
我建议您使用以下代码,这样您就可以通过在主线程(GUI 线程)上进行网络调用来专注于客户端/服务器通信,然后通过异步任务或任何其他线程机制正确执行操作:
//TODO:删除严格模式导入
StrictMode.ThreadPolicy 政策 = 新 StrictMode.ThreadPolicy.Builder().permitAll().build();
//TODO:确保列表视图在从 VideoViewerActivity 返回后得到正确更新
StrictMode.setThreadPolicy(政策);
为什么不这样定义方法:
@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;
}
}
我有一个接口,其方法定义如下:
@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" 附近的某处,但真的不知道是什么...
你应该在后台线程上进行网络调用:你有这个错误: android.os.NetworkOnMainThreadException
我建议您使用以下代码,这样您就可以通过在主线程(GUI 线程)上进行网络调用来专注于客户端/服务器通信,然后通过异步任务或任何其他线程机制正确执行操作:
//TODO:删除严格模式导入 StrictMode.ThreadPolicy 政策 = 新 StrictMode.ThreadPolicy.Builder().permitAll().build();
//TODO:确保列表视图在从 VideoViewerActivity 返回后得到正确更新 StrictMode.setThreadPolicy(政策);
为什么不这样定义方法:
@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;
}
}