英文:
Android java sending a JSON post request, body is empty
问题
private BroadcastReceiver onNotice = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
String packageName = intent.getStringExtra("package");
String titleData = intent.getStringExtra("title");
String textData = intent.getStringExtra("text");
TableRow tr = new TableRow(getApplicationContext());
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView textview = new TextView(getApplicationContext());
textview.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
textview.setTextSize(20);
textview.setTextColor(Color.parseColor("#0B0719"));
textview.setText(Html.fromHtml(packageName + "<br><b>" + titleData + " :</b>" + textData));
tr.addView(textview);
tab.addView(tr);
try {
URL url = new URL("https://[hidden].herokuapp.com/api");
try {
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; utf-8");
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("token", "secret");
con.setDoOutput(true);
JSONObject jObjectData = new JSONObject();
try {
jObjectData.put("package", packageName);
jObjectData.put("titleData", titleData);
jObjectData.put("textData", textData);
} catch (JSONException ex) {
System.out.println(ex);
}
System.out.println(jObjectData);
byte[] outputInBytes = jObjectData.toString().getBytes("UTF-8");
OutputStream os = con.getOutputStream();
os.write(outputInBytes);
os.close();
try (BufferedReader br = new BufferedReader(
new InputStreamReader(con.getInputStream(), "utf-8"))) {
StringBuilder response = new StringBuilder();
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
System.out.println(response.toString());
}
} catch (IOException ex) {
System.out.println(ex);
}
} catch (MalformedURLException ex) {
System.out.println(ex);
}
}
};
我知道代码中有些地方看起来比较混乱,这是因为我对Java和Android还很陌生,我只是想让这个简单的脚本能够工作。JSON对象是 jObjectData
,它将通知转发给API,问题是每当我发送请求时,请求的主体在我的Node.js Express后端中无法显示出来。请求的主体显示为空对象 {}
,我完全不知道如何解决这个问题。
英文:
I am trying to send a simple request using Java for Android. I have implemented a script and everything seems to be working fine the request is being sent but the most important part the JSON object just doesnt show up on the backend, the body of the request is completely empty.
private BroadcastReceiver onNotice = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
String packageName = intent.getStringExtra("package");
String titleData = intent.getStringExtra("title");
String textData = intent.getStringExtra("text");
TableRow tr = new TableRow(getApplicationContext());
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView textview = new TextView(getApplicationContext());
textview.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
textview.setTextSize(20);
textview.setTextColor(Color.parseColor("#0B0719"));
textview.setText(Html.fromHtml(packageName + "<br><b>" + titleData + " : </b>" + textData));
tr.addView(textview);
tab.addView(tr);
try {
URL url = new URL("https://[hidden].herokuapp.com/api");
try {
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; utf-8");
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("token", "secret");
con.setDoOutput(true);
JSONObject jObjectData = new JSONObject();
try {
jObjectData.put("package", packageName);
jObjectData.put("titleData", titleData);
jObjectData.put("textData", textData);
}catch(JSONException ex) {
System.out.println(ex);
}
System.out.println(jObjectData);
byte[] outputInBytes = jObjectData.toString().getBytes("UTF-8");
OutputStream os = con.getOutputStream();
os.write( outputInBytes );
os.close();
try (BufferedReader br = new BufferedReader(
new InputStreamReader(con.getInputStream(), "utf-8"))) {
StringBuilder response = new StringBuilder();
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
System.out.println(response.toString());
}
}catch (IOException ex){
System.out.println(ex);
}
} catch (MalformedURLException ex) {
System.out.println(ex);
}
}
};
I know some things are really messy its because i am very new to java and android, i just need to make this simple script work. The json object is jObjectData
which forwards a notification to api, the problem is that whenever i send the request its body wont show up on my Node.js Express backend. The body shows up as {}
, i am completely stuck how can i fix this?
答案1
得分: -1
由于某种原因,utf-8
导致了这个问题。
所以,代替
con.setRequestProperty("Content-Type", "application/json; utf-8");
我写了
con.setRequestProperty("Content-Type", "application/json");
现在它正常工作了,我不知道为什么会发生这种情况,也许在编码方面有一些交叉配置,导致我的服务器无法识别请求体。
英文:
For some reason the utf-8
causes this issue.
So instead of
con.setRequestProperty("Content-Type", "application/json; utf-8");
I wrote
con.setRequestProperty("Content-Type", "application/json");
and now it works, i have no idea why this is happening, maybe there's some cross configuration about the encoding that caused my server to not recognize the body.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论