JSON在Java中通过POST获取,但没有返回值。

huangapple go评论94阅读模式
英文:

JSON in java by POST getting None return

问题

以下是您要翻译的内容:

我通过以下代码请求POST方法
```try {
                    URL url = new URL("地址/discordnotifi");

                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

                    httpURLConnection.setReadTimeout(5000);

                    httpURLConnection.setConnectTimeout(5000);

                    httpURLConnection.setRequestMethod("POST");
                    httpURLConnection.connect();
                    JSONObject object = new JSONObject();
                    myPWord = ((EditText) (findViewById(R.id.edit_Id))).getText().toString();
                    object.put("token", tokens);
                    object.put("discordid", mydiscord);

                    OutputStream outputStream = httpURLConnection.getOutputStream();
                    outputStream.write(object.toString().getBytes("UTF-8"));

                    Log.d("debug",object.toString());
                    outputStream.flush();
                    outputStream.close();

而我的 tokensmydiscord 是我想要以 JSON 格式发送的信息,就像这样:
{"token":"tokens","discordid":"mydiscord"}

从 Python Flask 的部分如下:

@app.route('/discordnotifi', methods=['POST'])
def post():
    content = request.json
    print(content)
    Discordid = int(content["discordid"])
    token = content['token']
    """Discordid = request.form.get("discordid")
    token = request.form.get("token")"""
    print(Discordid, token)
    return ("谢谢")

在打印 content 时,我得到了 None,我真的不知道这里有什么问题。我原打算发送带有信息的 JSON,但我从 JSON 得到了 None。


<details>
<summary>英文:</summary>

I request POST by this code
```try {
                    URL url = new URL(&quot;adress/discordnotifi&quot;);

                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

                    httpURLConnection.setReadTimeout(5000);

                    httpURLConnection.setConnectTimeout(5000);

                    httpURLConnection.setRequestMethod(&quot;POST&quot;);
                    httpURLConnection.connect();
                    JSONObject object = new JSONObject();
                    myPWord = ((EditText) (findViewById(R.id.edit_Id))).getText().toString();
                    object.put(&quot;token&quot;, tokens);
                    object.put(&quot;discordid&quot;, mydiscord);

                    OutputStream outputStream = httpURLConnection.getOutputStream();
                    outputStream.write(object.toString().getBytes(&quot;UTF-8&quot;));

                    Log.d(&quot;debug&quot;,object.toString());
                    outputStream.flush();
                    outputStream.close();

and my tokens and mydiscord is the information that I want to send as JSON format like
{&quot;token&quot;:&quot;tokens&quot;,&quot;discordid&quot;:&quot;mydiscord&quot;}

and from python flask

@app.route(&#39;/discordnotifi&#39;, methods=[&#39;POST&#39;])
def post():
    content = request.json
    print(content)
    Discordid = int(content[&quot;discordid&quot;])
    token = content[&#39;token&#39;]
    &quot;&quot;&quot;Discordid = request.form.get(&quot;discordid&quot;)
    token = request.form.get(&quot;token&quot;)&quot;&quot;&quot;
    print(Discordid, token)
    return (&quot;Thx&quot;)

at print(content) I get None I really don't know whts wrong here. I was planning to send Json with information but I getting None from Json.

答案1

得分: 1

以下是您要翻译的内容:

你至少缺少的是在连接上启用输出。添加:

httpURLConnection.setDoOutput(true);

服务器可能还要求您设置一些标头,例如 content-type,以通知它您正在发送 JSON。

httpURLConnection.setRequestProperty("Content-Type", "application/json; utf-8");

如果您可以使用Java 11,考虑使用新的HttpClient类,而不是HttpUrlConnection。它简化了创建正确请求的过程。

英文:

The very least you're missing is enabling output on the connection. Add:

httpURLConnection.setDoOutput(true);

The server may also require that you set some headers, for example content-type to tell it that you are sending JSON.

httpURLConnection.setRequestProperty(&quot;Content-Type&quot;, &quot;application/json; utf-8&quot;);

If you can use Java 11, consider using the new HttpClient class instead of HttpUrlConnection. It simplifies creating correct requests.

huangapple
  • 本文由 发表于 2020年8月23日 22:42:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63548249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定