我的网络服务器响应 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 {}
如果我使用 curl 通过发布与我的 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);
}
更新 为了进一步清晰,我的 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 org.apache.http.message.BasicHttpResponse@44ec4978
I/ServiceRefreshData( 814): Created account errors ResponseJson {}
最佳答案
嗯,我认为你需要像这样指定一个标题:
HttpPost httppost = new HttpPost(url);
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-type", "application/json");
关于Android,解析 422 响应返回的 json 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15093564/