1

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

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
4

1 回答 1

0

重新启动 Android 设备有帮助。与 PC 相同的问题:Android,解析 422 响应返回的 json 时出现问题

谢施里坎特

于 2013-08-21T15:42:47.900 回答