【问题标题】:HttpURLConnection returns 422 code due to JSON由于 JSON,HttpURLConnection 返回 422 代码
【发布时间】:2013-08-21 15:08:22
【问题描述】:

没有postParameters(JSON 字符串,这当然是有效的)一切正常。但是当我添加postParameters 422 响应出现。

postParameters - {"password":"pass","email":"log"} - result of JSONObject.toString()

URL urlToRequest = new URL(SERVICE_URL + path);
urlConnection = (HttpURLConnection) urlToRequest.openConnection();

urlConnection.setConnectTimeout(CONNECTION_TIMEOUT);
urlConnection.setReadTimeout(DATARETRIEVAL_TIMEOUT);

urlConnection.setRequestMethod(requestMethod);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");

if(postParameters != null){
    urlConnection.setFixedLengthStreamingMode(postParameters.getBytes().length);
    PrintWriter out = new PrintWriter(urlConnection.getOutputStream());
    out.print(postParameters);
    out.flush();
    out.close();
}

int responseCode = urlConnection.getResponseCode();//422

【问题讨论】:

标签: android json networking httpurlconnection


【解决方案1】:

重启 Android 设备有帮助。 PC同样的问题:Android, Problems parsing json returned by a 422 response

感谢Shrikant

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    • 2018-06-30
    • 1970-01-01
    • 2016-04-09
    • 1970-01-01
    相关资源
    最近更新 更多