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

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

JSON in java by POST getting None return

问题

以下是您要翻译的内容:

  1. 我通过以下代码请求POST方法
  2. ```try {
  3. URL url = new URL("地址/discordnotifi");
  4. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  5. httpURLConnection.setReadTimeout(5000);
  6. httpURLConnection.setConnectTimeout(5000);
  7. httpURLConnection.setRequestMethod("POST");
  8. httpURLConnection.connect();
  9. JSONObject object = new JSONObject();
  10. myPWord = ((EditText) (findViewById(R.id.edit_Id))).getText().toString();
  11. object.put("token", tokens);
  12. object.put("discordid", mydiscord);
  13. OutputStream outputStream = httpURLConnection.getOutputStream();
  14. outputStream.write(object.toString().getBytes("UTF-8"));
  15. Log.d("debug",object.toString());
  16. outputStream.flush();
  17. outputStream.close();

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

从 Python Flask 的部分如下:

  1. @app.route('/discordnotifi', methods=['POST'])
  2. def post():
  3. content = request.json
  4. print(content)
  5. Discordid = int(content["discordid"])
  6. token = content['token']
  7. """Discordid = request.form.get("discordid")
  8. token = request.form.get("token")"""
  9. print(Discordid, token)
  10. return ("谢谢")

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

  1. <details>
  2. <summary>英文:</summary>
  3. I request POST by this code
  4. ```try {
  5. URL url = new URL(&quot;adress/discordnotifi&quot;);
  6. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  7. httpURLConnection.setReadTimeout(5000);
  8. httpURLConnection.setConnectTimeout(5000);
  9. httpURLConnection.setRequestMethod(&quot;POST&quot;);
  10. httpURLConnection.connect();
  11. JSONObject object = new JSONObject();
  12. myPWord = ((EditText) (findViewById(R.id.edit_Id))).getText().toString();
  13. object.put(&quot;token&quot;, tokens);
  14. object.put(&quot;discordid&quot;, mydiscord);
  15. OutputStream outputStream = httpURLConnection.getOutputStream();
  16. outputStream.write(object.toString().getBytes(&quot;UTF-8&quot;));
  17. Log.d(&quot;debug&quot;,object.toString());
  18. outputStream.flush();
  19. 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

  1. @app.route(&#39;/discordnotifi&#39;, methods=[&#39;POST&#39;])
  2. def post():
  3. content = request.json
  4. print(content)
  5. Discordid = int(content[&quot;discordid&quot;])
  6. token = content[&#39;token&#39;]
  7. &quot;&quot;&quot;Discordid = request.form.get(&quot;discordid&quot;)
  8. token = request.form.get(&quot;token&quot;)&quot;&quot;&quot;
  9. print(Discordid, token)
  10. 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

以下是您要翻译的内容:

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

  1. httpURLConnection.setDoOutput(true);

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

  1. 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:

  1. 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.

  1. 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:

确定