如何从Android应用程序发布节点数据到drupal服务器?

时间:2015-02-26 06:29:05

标签: android web-services drupal-7

The attached screen shot shows the node JSON data format on drupal server

现在我想从android应用程序创建这个节点。为此,我通过将它们放入json对象以键值对的形式发送数据。当我发布这个json对象时,我收到了这些错误:

  

HTTP / 1.1 401未经授权: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;

2 个答案:

答案 0 :(得分:0)

步骤1.在/services/session/token

获取会话令牌

步骤2.在执行X-CSRF-Token=SessionTokenHere

之前设置包含httpPost.setEntity(stringEntity);的标头

答案 1 :(得分:0)

因为您使用JSON发送数据
httpPost.setHeader(" Content-Type"," application / x-www-form-urlencoded");
必须是httpPost.setHeader(" Content-Type"," application / json");