Android - REST API帖子,我的标题需要什么

时间:2016-08-12 08:23:56

标签: android api rest cookies drupal

我有一个Android应用程序,它应该通过REST API将数据发布到我的Drupal站点。

我有登录。
从此登录,我可以获得会话名称,会话ID和令牌。

我的问题。
你如何在登录后发帖。 我是否需要将会话ID,会话名称和令牌添加到标头中? 当我发帖时,我是否还应该在标题中添加用户名和密码 饼干怎么样? (会话ID和会话名称是否构成cookie?)
如何将cookie添加到标题中。

这是我到目前为止所拥有的。
onPostExecute,我从结果中得到这条消息 节点类型是必需的

我一直在努力解决这个问题。 如果您有任何建议或知道任何好的示例/教程,请告诉我。

提前致谢

public String session_name;
public String session_id;
public String token;

private class JsonPostRequest extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... voids) {
        String address = "http://app.flickgo.com/apistuff/node.json";
        HttpURLConnection urlConnection;

        CookieManager cookies = new CookieManager();
        cookies.getCookieStore().add(null, new HttpCookie("app.flickgo.com", "/"));
        cookies.getCookieStore().add(null, new HttpCookie("has_js", "1"));
        cookies.getCookieStore().add(null, new HttpCookie(session_name, session_id));

        try {
            URL url = new URL(address);
            urlConnection = (HttpURLConnection) url.openConnection();
            String userCredentials = "my_username:my_password";
            String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
            urlConnection.setRequestProperty ("Authorization", basicAuth);
            urlConnection.setRequestProperty("X-CSRF-Token", token);
            urlConnection.setRequestMethod("POST");
            urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);

            OutputStream outputStream = new BufferedOutputStream(urlConnection.getOutputStream());
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8"));

            String title = "Mobile";
            String body = "Mobile body";
            String input = ("{ \"title\":\""+title+"\",\"type\":\"article\",\"body\":{\"und\":[{ \"value\":\""+body+"\"}]}}");

            writer.write(input);
            writer.flush();
            writer.close();
            outputStream.close();

            JSONObject jsonObject = new JSONObject();
            InputStream inputStream;
            // get stream
            if (urlConnection.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
                inputStream = urlConnection.getInputStream();
            } else {
                inputStream = urlConnection.getErrorStream();
            }
            // parse stream
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String temp, response = "";
            while ((temp = bufferedReader.readLine()) != null) {
                response += temp;
            }
            // put into JSONObject
            jsonObject.put("Content", response);
            jsonObject.put("Message", urlConnection.getResponseMessage());
            jsonObject.put("Length", urlConnection.getContentLength());
            jsonObject.put("Type", urlConnection.getContentType());

            return jsonObject.toString();
        } catch (IOException | JSONException e) {
            return e.toString();
        }
    }


    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        Toast.makeText(LoginActivity.this, result, Toast.LENGTH_LONG).show();
    }
}

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。 这就是我添加cookie的方式。

urlConnection.setRequestProperty("Cookie",session_name+"="+session_id);