Как отправить данные узла на сервер drupal из приложения Android?
Теперь я хочу создать этот узел из приложения для Android. Для этого я отправляю данные в виде пар ключевых значений, помещая их в объект json. Когда я отправляю этот объект JSON я получаю эти ошибки:
HTTP/1.1 401 Несанкционированная: проверка CSRF не удался & [ "CSRF проверки Failed"]
Я использую этот код:
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;
Я уже устанавливаю токен сеанса, cookie и т. Д. Перед блоком try выше указанного кода. Код выглядит следующим образом: –
HttpPost httpPost = new HttpPost ("url"); \t \t httpPost.setHeader («Accept», «application/json»); \t \t httpPost.setHeader («Content-Type», «application/x-www-form-urlencoded»); \t \t \t httpPost.setHeader («X-CSRF-токен», токен); \t \t httpPost.setHeader ("Cookie", session_name + "=" + session_id); –