【问题标题】:How to post a node data to drupal server from android application?如何从android应用程序将节点数据发布到drupal服务器?
【发布时间】: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


    【解决方案1】:

    第一步:在/services/session/token获取会话令牌

    步骤 2. 在执行 httpPost.setEntity(stringEntity); 之前设置包含 X-CSRF-Token=SessionTokenHere 的标头

    【讨论】:

    • 我已经在上面给定代码的 try 块之前设置了会话令牌、cookie 等。代码如下:
    • HttpPost httpPost = new HttpPost("url"); httpPost.setHeader("接受", "应用程序/json"); httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); httpPost.setHeader("X-CSRF-Token", token); httpPost.setHeader("Cookie", session_name + "=" + session_id);
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-11
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-15
      相关资源
      最近更新 更多