2013-02-26 86 views
2

我的web服务器响应422不可处理的实体错误,并呈现带有错误列表 的错误列表的json响应。Android,解析由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 {} 

如果我使用卷曲张贴完全相同的消息我的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); 
    } 

UPDATE为了更清楚我的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); 
    } 
} 

为完整的代码在我的网络服务器(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 

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 [email protected] 
I/ServiceRefreshData( 814): Created account errors ResponseJson {} 
+0

你有没有尝试打印'responseJson'而不是JsonObject,并检查你得到的回应是什么? – 2013-02-26 16:05:59

+0

@ρяσѕρєяK我用更多的日志记录信息更新了我的问题 – jamesc 2013-02-26 16:29:41

+0

Hmmmm是您的服务器实际发送JSON中的任何错误?你的客户似乎认为它只是'{}'... – dmon 2013-02-26 16:33:14

回答

0

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

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

0

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

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

我的问题是响应不是发送数据,我应该指出你的标题是错误的,因为它们没有考虑到UTF8。为了清晰起见,尽管在我的问题中包含了我的setHeaders方法。不管怎么说,还是要谢谢你 – jamesc 2013-02-26 16:18:30