Android,解析由422响应返回的json的问题

时间:2013-02-26 15:56:31

标签: android json

我的Web服务器以422不可处理的实体错误响应,并使用错误列表呈现json响应 e.g。

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

以上Log消息的以下输出是

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作为发布请求的结果

    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 {}

2 个答案:

答案 0 :(得分:0)

我认为你需要指定一个这样的标题:

HttpPost httppost = new HttpPost(url);
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-type", "application/json");

答案 1 :(得分:0)

重新启动电脑后,问题就消失了。这整集都让我很困惑。我确信我的代码是正确的,日志输出证明它不是。我只能把它归结为一些完全不可思议的缓存问题。

非常感谢那些帮助过的人。