我的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 {}
你有沒有嘗試打印'responseJson'而不是JsonObject,並檢查你得到的迴應是什麼? – 2013-02-26 16:05:59
@ρяσѕρєяK我用更多的日誌記錄信息更新了我的問題 – jamesc 2013-02-26 16:29:41
Hmmmm是您的服務器實際發送JSON中的任何錯誤?你的客戶似乎認爲它只是'{}'... – dmon 2013-02-26 16:33:14