Android,Rest api发布错误-访问被拒绝

Android,Rest api发布错误-访问被拒绝,android,rest,drupal,Android,Rest,Drupal,我是初学者。这是我的第一个Android应用程序。我在从Android向Drupal服务器发布数据时遇到问题。我正在使用RESTAPI 我可以作为Drupal管理员用户登录,我得到会话id、会话名称和令牌。我的问题是发布数据。我认为问题在于发布时的身份验证。我不知道该怎么做那部分 INTERNET和ACCESS_NETWORK_状态都在清单中声明 登录部分(工作) 私有类登录过程扩展了异步任务{ @凌驾 受保护的字符串背景(无效…无效){ 字符串地址=”http://app.flickgo.co

我是初学者。这是我的第一个Android应用程序。我在从Android向Drupal服务器发布数据时遇到问题。我正在使用RESTAPI

我可以作为Drupal管理员用户登录,我得到会话id、会话名称和令牌。我的问题是发布数据。我认为问题在于发布时的身份验证。我不知道该怎么做那部分

INTERNET和ACCESS_NETWORK_状态都在清单中声明

登录部分(工作)

私有类登录过程扩展了异步任务{
@凌驾
受保护的字符串背景(无效…无效){
字符串地址=”http://app.flickgo.com/apistuff/user/login.json";
HttpURLConnection-urlConnection;
字符串请求体;
Uri.Builder=新的Uri.Builder();
Map params=新的HashMap();
参数put(“用户名”、“我的用户名”);
参数put(“密码”、“我的密码”);
//编码参数
迭代器条目=params.entrySet().Iterator();
while(entries.hasNext()){
Map.Entry=(Map.Entry)entries.next();
builder.appendQueryParameter(entry.getKey().toString(),entry.getValue().toString());
条目。删除();
}
requestBody=builder.build().getEncodedQuery();
试一试{
URL=新的URL(地址);
urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setDoOutput(true);
setRequestProperty(“内容类型”,“应用程序/x-www-form-urlencoded”);
OutputStream OutputStream=新的BufferedOutputStream(urlConnection.getOutputStream());
BufferedWriter writer=新的BufferedWriter(新的OutputStreamWriter(outputStream,utf-8));
writer.write(请求体);
writer.flush();
writer.close();
outputStream.close();
JSONObject JSONObject=新的JSONObject();
输入流输入流;
//获取流
if(urlConnection.getResponseCode()

发布部分,不工作。
在第二个活动中,我想发布一些数据。 这就是我的问题所在。我一直收到拒绝访问的消息。

如何使用result(intent.putExtra(“My_result”,result)-来自登录页面)中的会话id、会话名称或令牌发布内容?这真的是正确的方法吗?如果有更好的办法,请告诉我

private class JsonPostRequest extends AsyncTask<Void, Void, String> {
    @Override
    protected String doInBackground(Void... voids) {
        try {
            String address = "http://app.flickgo.com/apistuff/node.json";
            JSONObject json = new JSONObject();
            json.put("title", "Dummy Title");
            json.put("type", "article");
            String requestBody = json.toString();
            URL url = new URL(address);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            urlConnection.setDoOutput(true);
            urlConnection.setRequestProperty("Content-Type", "application/json");
            OutputStream outputStream = new BufferedOutputStream(urlConnection.getOutputStream());
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8"));
            writer.write(requestBody);
            writer.flush();
            writer.close();
            outputStream.close();

            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 jsonObject = new 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 + "Test", Toast.LENGTH_LONG).show();
        //Log.i(LOG_TAG, "POST RESPONSE: " + result);
        //mTextView.setText(result);
    }
}
私有类JsonPostRequest扩展了异步任务{
@凌驾
受保护的字符串背景(无效…无效){
试一试{
字符串地址=”http://app.flickgo.com/apistuff/node.json";
JSONObject json=新的JSONObject();
put(“标题”、“虚拟标题”);
put(“type”、“article”);
字符串requestBody=json.toString();
URL=新的URL(地址);
HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setDoOutput(true);
setRequestProperty(“内容类型”、“应用程序/json”);
OutputStream OutputStream=新的BufferedOutputStream(urlConnection.getOutputStream());
BufferedWriter writer=新的BufferedWriter(新的OutputStreamWriter(outputStream,utf-8));
writer.write(请求体);
writer.flush();
writer.close();
outputStream.close();
输入流输入流;
//获取流
if(urlConnection.getResponseCode()private class JsonPostRequest extends AsyncTask<Void, Void, String> {
    @Override
    protected String doInBackground(Void... voids) {
        try {
            String address = "http://app.flickgo.com/apistuff/node.json";
            JSONObject json = new JSONObject();
            json.put("title", "Dummy Title");
            json.put("type", "article");
            String requestBody = json.toString();
            URL url = new URL(address);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            urlConnection.setDoOutput(true);
            urlConnection.setRequestProperty("Content-Type", "application/json");
            OutputStream outputStream = new BufferedOutputStream(urlConnection.getOutputStream());
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8"));
            writer.write(requestBody);
            writer.flush();
            writer.close();
            outputStream.close();

            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 jsonObject = new 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 + "Test", Toast.LENGTH_LONG).show();
        //Log.i(LOG_TAG, "POST RESPONSE: " + result);
        //mTextView.setText(result);
    }
}
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setRequestMethod("POST");
    urlConnection.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());
    wr.writeBytes(requestBody);
    wr.flush();
    wr.close();

    InputStream inputStream = urlConnection.getInputStream();
    // ..
Map<String, Object> params = new HashMap<>();
params.put("username", "myUsername");
params.put("password", "myPassword");
postLogin(getApplicationContext(),"http://app.flickgo.com/apistuff/node.json",params)

public JSONObject postLogin(Context mContext, String REQUEST_URL,Map<String, Object> params) {
                JSONObject jsonObject = null;
                BufferedReader reader = null;
                try {
                    URL url = new URL(REQUEST_URL);
                    StringBuilder postData = new StringBuilder();

                    for (Map.Entry<String, Object> param : params.entrySet()) {
                        if (postData.length() != 0) postData.append('&');
                        postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
                        postData.append('=');
                        postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
                    }
                    byte[] postDataBytes = postData.toString().getBytes("UTF-8");

                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                connection.setRequestProperty("Authorization", token); //Set your token
                    connection.setConnectTimeout(8000);
                    connection.setRequestMethod("POST");
                    connection.setUseCaches(false);
                    connection.setDoOutput(true);
                    connection.getOutputStream().write(postDataBytes);
                    connection.connect();
                    StringBuilder sb;
                    int statusCode = connection.getResponseCode();
                    if (statusCode == 200) {
                        sb = new StringBuilder();
                        reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                        String line;
                        while ((line = reader.readLine()) != null) {
                            sb.append(line);
                        }
                        jsonObject = new JSONObject(sb.toString());
                    }
                    connection.disconnect();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
                return jsonObject;
            }
urlConnection.setRequestProperty("Cookie",session_name+"="+session_id);