我的web服务器响应422不可处理的实体错误,并呈现带有错误列表 的错误列表的json响应。Android,解析由422响应返回的json的问题
{"name":["has already been taken"]}
由于某种原因,虽然我的android应用程序拒绝承认,在响应中有任何json。
HttpResponse response = HttpRequest.httpPostJSONObject(this, Urls.getUrlForAccountTeams(this), tm.getJsonObject(), token);
int code = response.getStatusLine().getStatusCode();
if (code == 422){
String responseJson = EntityUtils.toString(response.getEntity());
JSONObject responseEntity = new JSONObject(responseJson);
Log.i(TAG, "Created account errors " + responseEntity.toString());
}
从上面的日志信息下面的输出是
I/ServiceRefreshData( 780): Server response 422
I/ServiceRefreshData( 780): Created account errors {}
如果我使用卷曲张贴完全相同的消息我的Android应用程序来模拟这种派遣我得到的JSON如上图{"name":["has already been taken"]}
这正是我期待在我的Android应用程序中看到的
关于如何获取json的任何想法。我在与解析全成JSON响应
请求发送JSON没有问题,使用org.apache.http.HttpResponse的POST请求的结果
public static HttpResponse httpPostJSONObject(Context context, String url, JSONObject data, String token) throws ClientProtocolException, IOException{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
setHeaders(httppost, token);
StringEntity se = new StringEntity(data.toString());
httppost.setEntity(se);
return httpclient.execute(httppost);
}
UPDATE为了更清楚我的setHeaders方法看起来像这样
private static void setHeaders(HttpRequestBase httpget, String token) {
httpget.addHeader("Accept", "application/json");
httpget.addHeader("Content-Type", "application/json; charset=UTF-8");
if(token != null){
httpget.addHeader("Authorization", "Token token="+token);
}
}
为完整的代码在我的网络服务器(Rails的3.2.12),它生产这个JSON是
respond_to do |format|
if @team.save
format.html { redirect_to @team, notice: 'Team was successfully created.' }
format.json { render json: @team, status: :created, location: [:api, @team] }
else
format.html { render action: "new" }
format.json { render json: @team.errors, status: :unprocessable_entity }
end
end
UPDATE有更多的调试信息按评论 结果改变了我的日志消息,所以我的方法现在发送的数据看起来像这样
try {
HttpResponse response = HttpRequest.httpPostJSONObject(this, Urls.getUrlForAccountTeams(this), tm.getJsonObject(), token);
int code = response.getStatusLine().getStatusCode();
Log.i(TAG, "Server response " + code);
if (code == 422){
String responseJson = EntityUtils.toString(response.getEntity());
JSONObject responseEntity = new JSONObject(responseJson);
Log.i(TAG, "Created account errors Response Entity " + responseEntity.toString());
Log.i(TAG, "Created account errors Response " + response.toString());
Log.i(TAG, "Created account errors ResponseJson " + responseJson.toString());
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
,并产生
I/ServiceRefreshData( 814): Server response 422
I/ServiceRefreshData( 814): Created account errors Response Entity {}
I/ServiceRefreshData( 814): Created account errors Response [email protected]
I/ServiceRefreshData( 814): Created account errors ResponseJson {}
你有没有尝试打印'responseJson'而不是JsonObject,并检查你得到的回应是什么? – 2013-02-26 16:05:59
@ρяσѕρєяK我用更多的日志记录信息更新了我的问题 – jamesc 2013-02-26 16:29:41
Hmmmm是您的服务器实际发送JSON中的任何错误?你的客户似乎认为它只是'{}'... – dmon 2013-02-26 16:33:14