【问题标题】:Unable to solve POST request in Android无法解决 Android 中的 POST 请求
【发布时间】:2020-05-22 19:05:56
【问题描述】:

我知道人们已经多次问过这个问题。但它仍然给我带来了困难。

从几个地方收集代码后:比如Tutorial 我可以写这个。

我做了什么: 我已经检查了 GET 请求上代码的工作情况。它正在工作。

private class Myworker extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... voids) {
        try {
            URL url = new URL("myurl");
            org.json.JSONObject df = new org.json.JSONObject();
            df.put("amount", "50");

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(15000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);

            try {
                OutputStream os = conn.getOutputStream();
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
                writer.write(getPostDataString(df));

                writer.flush();
                writer.close();
                os.close();

                int responseCode = conn.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    StringBuilder stringBuilder = new StringBuilder();
                    String line;
                    while ((line = bufferedReader.readLine()) != null) {
                        stringBuilder.append(line).append("\n");
                    }
                    bufferedReader.close();
                    System.out.println("n " + new String(stringBuilder));
                }
            } finally {
                conn.disconnect();
            }
        } catch (Exception e) {
            System.out.println("Unable *******");
            e.printStackTrace();
        }
        return null;
    }
}

    public String getPostDataString(org.json.JSONObject params) throws Exception {
        StringBuilder result = new StringBuilder();
        boolean first = true;

        Iterator itr = params.keys();

        while (itr.hasNext()) {
            String key = (String) itr.next();
            Object value = params.get(key);

            if (first)
                first = false;
            else
                result.append("&");

            result.append(URLEncoder.encode(key, "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode(value.toString(), "UTF-8"));
        }
        return result.toString();
    }
}

错误: int responseCode=conn.getResponseCode() 也作为 415 返回。

D/NetworkSecurityConfig: No Network Security Config specified, using platform default

请帮忙

【问题讨论】:

    标签: java android api post request


    【解决方案1】:

    A 415 is Unsupported Media Type,这可能是由于您没有正确设置 Content-Type 标头。

    尝试添加

    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    

    将来您可能想尝试使用 http 客户端库,例如 apache httpcomponents 或 google-http-client,因为它们更易于使用。

    【讨论】:

    • 嘿@Deadron 谢谢你的回答......但它没有工作......同样的错误...... 415......并从你的评论中得到提示我也尝试了 application/json ,它给了错误 400。我还能做什么?
    • 你不能告诉服务器你正在发送 json 并期望它能够解析不是 json 的东西。 400 是朝着正确方向迈出的一步。它告诉您您的请求正文要么缺少某些内容,要么格式不正确。如果您阅读响应正文可能会有所帮助。
    • 没有理想的格式。这取决于服务器接受什么。大多数服务器都接受 form-urlencoded,但这并不能保证。
    • 没有理想的格式。这取决于服务器接受什么。 --- 好的
    • 嘿兄弟,..弄脏了它...以字节为单位编写了参数..它起作用了...:P ;P IDK为什么..
    猜你喜欢
    • 2016-12-11
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    相关资源
    最近更新 更多