3

My web server responds with a 422 unprocessable entity error and renders a json response with a list of the errors e.g.

{"name":["has already been taken"]}

For some reason though My android app refuses to acknowledge that there is any json whatsoever in the response.

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

The following output from the above Log message is

I/ServiceRefreshData(  780): Server response 422
I/ServiceRefreshData(  780): Created account errors {}

If I use curl to emulate this by posting exactly the same message as my android app is sending I get the json as shown above {"name":["has already been taken"]} and this is exactly what I am expecting to see in my android app

Any ideas on how to get the json. I'm having no issues with parsing successfull json responses

The request to send the json uses org.apache.http.HttpResponse for the result of the post request

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

UPDATE For further clarity my setHeaders method looks like this

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

For completeness the code on my webserver (Rails 3.2.12) that is producing this json is

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 with more debugging info as per comment Result of changing my log messages so my method for sending the data now looks like this

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

And produces

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 {}
jamesc
  • 12,423
  • 15
  • 74
  • 113

2 Answers2

0

Well i think that you need to specify a header like this:

HttpPost httppost = new HttpPost(url);
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-type", "application/json");
Jorge Osorio
  • 31
  • 1
  • 5
  • My issue is with the response not the the sending of the data and I should point out that your headers are wrong as they don't take into account UTF8. For clarity though to include my setHeaders method in my question. Thanks anyway – jamesc Feb 26 '13 at 16:18
0

After re-booting my PC The problem just went away. I am really puzzled by this whole episode. I was convinced my code was right and the log output was proving it wasn't. I can only put this down to some totally unfathomable caching issue.

A big thank you to those that helped.

jamesc
  • 12,423
  • 15
  • 74
  • 113