3

我的 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 {}
4

2 回答 2

0

好吧,我认为您需要指定这样的标题:

HttpPost httppost = new HttpPost(url);
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-type", "application/json");
于 2013-02-26T16:06:42.197 回答
0

重新启动我的电脑后问题就消失了。我真的对这整个情节感到困惑。我确信我的代码是正确的,并且日志输出证明它不是。我只能把它归结为一些完全深不可测的缓存问题。

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

于 2013-02-26T17:43:46.143 回答