【发布时间】:2013-02-12 03:59:44
【问题描述】:
我的 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
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 {}
【问题讨论】:
-
您是否尝试打印
responseJson而不是 JsonObject 并检查您得到的响应? -
@ρяσѕρєяK 我用更多日志信息更新了我的问题
-
嗯,您的服务器实际上是在发送 JSON 中的任何错误吗?您的客户似乎认为这只是
{}... -
@jamesw :检查您作为
response.getEntity().getContentLength()响应的内容长度。确保它大于 3 表示您正在获取数据,或者 -1 或小于 3 表示您需要查看在您的服务器端代码。谢谢 -
@ρяσѕρєяK 内容长度为-1