Android,解析422响应返回的json时出现问题

Android,解析422响应返回的json时出现问题,android,json,Android,Json,我的web服务器响应一个422不可处理实体错误,并呈现一个带有错误列表的json响应 e、 g 出于某种原因,尽管我的android应用程序拒绝承认响应中有任何json HttpResponse response = HttpRequest.httpPostJSONObject(this, Urls.getUrlForAccountTeams(this), tm.getJsonObject(), token); int code = response.getStatusLine().getSta

我的web服务器响应一个422不可处理实体错误,并呈现一个带有错误列表的json响应 e、 g

出于某种原因,尽管我的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”:[“have have behave”]}
,这正是我希望在我的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);
    }
}
        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();
        }
为完整起见,在我的Web服务器(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
根据评论更新更多调试信息 更改我的日志消息以使我发送数据的方法现在如下所示的结果

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);
    }
}
        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");

重新启动电脑后,问题就消失了。我真的被这整件事弄糊涂了。我确信我的代码是正确的,而日志输出证明它不是。我只能把这归结为一些完全深不可测的缓存问题


非常感谢您的帮助。

您是否尝试打印
responseJson
而不是JsonObject,并检查您得到了什么响应?@ρ;;K我已使用更多日志信息更新?您的客户似乎认为这只是
{}
..@jamesw:检查您在响应中得到的内容长度,即
response.getEntity().getContentLength()
。确保它大于3表示您正在获取数据,或者如果-1或小于3表示您需要查看服务器端代码。谢谢@ρ∑ρѕρєK内容长度为-1,我的问题在于响应而不是发送数据,我应该指出您的标题是错误的,因为它们没有考虑UTF8。为了清楚起见,我想在我的问题中包括我的setHeaders方法。无论如何谢谢你