java - HttpURLConnection - 响应代码 : 400 (Bad Request) Android Studio -> xserve

标签 java android httpurlconnection xserver

所以我正在尝试通过 Xserve 连接到我们的数据库,目前我正在尝试访问用户的 token 。我使用正确的用户名和密码以及上下文类型和授权类型;我知道这一点是因为我已经通过谷歌邮政管理员扩展尝试了相同的 POST 方法。无论出于何种原因,当我在 Android 上尝试相同的操作时,至少我认为是相同的,它给了我一个 400 响应代码,并且不返回任何内容。

这是用于连接的代码:

private HttpURLConnection urlConnection;

@Override
    protected Boolean doInBackground(Void... params) {
        Boolean blnResult = false;
        StringBuilder result = new StringBuilder();
        JSONObject passing = new JSONObject();

        try {
            URL url = new URL("http://xserve.uopnet.plymouth.ac.uk/modules/INTPROJ/PRCS251M/token");

            // set up connection
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            urlConnection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8" );
            urlConnection.setRequestMethod("POST");
            urlConnection.connect();

            // set up parameters to pass
            passing.put("username", mEmail);
            passing.put("password", mPassword);
            passing.put("grant_type", "password");

            // add parameters to connection
            OutputStreamWriter wr= new OutputStreamWriter(urlConnection.getOutputStream());
            wr.write(passing.toString());


            // If request was good
            if (urlConnection.getResponseCode() == 200) {
                blnResult = true;
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(urlConnection.getInputStream()));
                String line;

                while ((line = reader.readLine()) != null) {
                    result.append(line);
                }
                reader.close();
            }

            //JSONObject json = new JSONObject(builder.toString());
            Log.v("Response Code", String.format("%d", urlConnection.getResponseCode()));
            Log.v("Returned String", result.toString());

        }catch( Exception e) {
            e.printStackTrace();
        }
        finally {
            urlConnection.disconnect();
        }

        return blnResult;
    }

我还没有将结果存储到 JSONObject 中,因为稍后我将使用它,但我期望通过“Log.v”得到某种输出。

有什么突出的地方吗?

最佳答案

 try {
        URL url = new URL("http://xserve.uopnet.plymouth.ac.uk/modules/INTPROJ/PRCS251M/token");

        parameters = new HashMap<>();
        parameters.put("username", mEmail);
        parameters.put("password", mPassword);
        parameters.put("grant_type", "password");

        set = parameters.entrySet();
        i = set.iterator();
        postData = new StringBuilder();

        for (Map.Entry<String, String> param : parameters.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"));
        }

        postDataBytes = postData.toString().getBytes("UTF-8");

        // set up connection
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);
        urlConnection.setConnectTimeout(5000);
        urlConnection.setReadTimeout(5000);
        urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        urlConnection.setRequestMethod("POST");
        urlConnection.getOutputStream().write(postDataBytes);

        // If request was good
        if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            reader = new BufferedReader(
                    new InputStreamReader(urlConnection.getInputStream()));
            String line;

            while ((line = reader.readLine()) != null) {
                result.append(line);
            }
            reader.close();
        }

        Log.v("Login Response Code", String.valueOf(urlConnection.getResponseCode()));
        Log.v("Login Response Message", String.valueOf(urlConnection.getResponseMessage()));
        Log.v("Login Returned String", result.toString());


        jsonObject = new JSONObject(result.toString());
        token = jsonObject.getString("access_token");

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        urlConnection.disconnect();
        if (token != null) {
            jsonObject = driverInfo(token);

        }
    }

这有效,尽管我现在已将其移至其自己的函数中。 将输入类型更改为 HashMap

关于java - HttpURLConnection - 响应代码 : 400 (Bad Request) Android Studio -> xserve,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42958906/

相关文章:

android - 使用选择器,形状不会出现在 ImageButton 上

java - Android:目录和子目录列出连续 Activity 问题

android - HttpURLConnection 使用 FileNotFoundException 将 POST 发送到 Web 服务

android - 未经互联网许可与服务器通信

java - 缺少 Maven 插件

java - 在哪里放置相对于 java 项目的声音文件?

java - Deeplearning4j 在 CNN 中使用 LibSVM 文件的困难

android - AsyncTask 的行为

java - Android中的HttpUrlConnection设置范围被忽略

java - 从 Gridpane 中删除 ImageView