英文:
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();
而我的 tokens 和 mydiscord 是我想要以 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("adress/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();
and my tokens and mydiscord is the information that I want to send as JSON format like
{"token":"tokens","discordid":"mydiscord"}
and from 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 ("Thx")
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("Content-Type", "application/json; utf-8");
If you can use Java 11, consider using the new HttpClient class instead of HttpUrlConnection. It simplifies creating correct requests.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论