我的 Web 服务器以 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);
}
}
为了完整起见,生成此 json 的网络服务器(Rails 3.2.12)上的代码是
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
根据评论更新更多调试信息更改我的日志消息的结果,因此我发送数据的方法现在看起来像这样
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 {}