【发布时间】:2015-02-26 06:29:04
【问题描述】:
现在我想从 android 应用程序创建这个节点。为此,我通过将它们放入 json 对象中以键值对的形式发送数据。当我发布这个 json 对象时,我收到了这些错误:
HTTP/1.1 401 Unauthorized : CSRF 验证失败 & ["CSRF 验证失败"]
我正在使用此代码:
try {
JSONObject birthDateJSONObject = new JSONObject();
birthDateJSONObject.put("day", 28);
birthDateJSONObject.put("month", 02);
birthDateJSONObject.put("year", 2015);
JSONObject endDateJSONObject = new JSONObject();
endDateJSONObject.put("day", 28);
endDateJSONObject.put("month", 02);
endDateJSONObject.put("year", 2015);
JSONObject jsonObject = new JSONObject();
jsonObject.put("title", titleEditText.getText().toString().trim());
jsonObject.put("type", "charity");
jsonObject.put("uid", uid);
jsonObject.put("charity_amount", 200);
jsonObject.put("person_birthdate", birthDateJSONObject);
jsonObject.put("charity_org", organizationEditText.getText().toString().trim());
jsonObject.put("message", messageEditText.getText().toString().trim());
jsonObject.put("person_registered", false);
jsonObject.put("charity_enddate", endDateJSONObject);
StringEntity stringEntity = new StringEntity(jsonObject.toString());
stringEntity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(stringEntity);
}
catch (UnsupportedEncodingException | JSONException e) {
Log.d("Encoding Exception",e.toString());
}
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
if(httpResponse.getStatusLine() != null && httpResponse.getStatusLine().getStatusCode() == 401) {
Log.d("Submission failed ", httpResponse.getStatusLine().toString());
}
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
String json = sb.toString();
Log.d("Response", json);
}
catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
}
catch (ClientProtocolException e) {
Log.d("Protocol Exception",e.toString());
}
catch (IOException e) {
Log.d("Protocol Exception",e.toString());
}
return null;
【问题讨论】:
标签: android web-services drupal-7