gpt4 book ai didi

java - 服务器发送请求 firebase java.net.protocolException 在读取响应后无法写入请求正文

转载 作者:太空宇宙 更新时间:2023-11-04 10:18:46 25 4
gpt4 key购买 nike

我正在尝试使用 firebase 发送通知...7天前代码正在工作,但现在每当我调用 conn.getOutputStream();

时,我得到的只是 (java.net.protocolException在读取响应后无法写入请求正文)

谢谢大家的帮助

URL url = null;
try {
url = new URL("https://fcm.googleapis.com/fcm/send");
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Authorization","key=someKey");
conn.setRequestProperty("Content-Type", "application/json; UTF-8");
JSONObject data= new JSONObject();
JSONObject jsonObject=new JSONObject();
try {
jsonObject.put("to","/topics/all");
data.put("title",title.getText());
data.put("messages",message.getText());
data.put("body",message.getText());
if(spinner.getSelectedItemPosition()==1)data.put("getId",videoListId.getText().toString());
else if(spinner.getSelectedItemPosition()==2)data.put("getId",videoId.getText().toString());
if(spinner.getSelectedItemPosition()==1)data.put("getSubject",videoListSubject.getText().toString());
else if(spinner.getSelectedItemPosition()==2)data.put("getSubject",videoSubject.getText().toString());
if(spinner.getSelectedItemPosition()!=0) data.put("getActivity",spinner.getSelectedItem().toString());
data.put("img_url",downloadUri.toString());
jsonObject.put("data",data);
OutputStreamWriter wr=new OutputStreamWriter(conn.getOutputStream());//exception here
wr.write(String.valueOf(jsonObject));
wr.flush();

conn.getInputStream();
wr.close();
} catch (JSONException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}

最佳答案

我使用volley库解决了这个问题,这是我的解决方案......谢谢大家

void jsonRequest() {


URL url = null;
try {
url = new URL("https://fcm.googleapis.com/fcm/send");
try {
JSONObject data= new JSONObject();
JSONObject jsonObject=new JSONObject();
jsonObject.put("to","someone");
data.put("title",title.getText());
data.put("messages",message.getText());
data.put("body",message.getText());
if(spinner.getSelectedItemPosition()==1)data.put("getId",videoListId.getText().toString());
else if(spinner.getSelectedItemPosition()==2)data.put("getId",videoId.getText().toString());
if(spinner.getSelectedItemPosition()==1)data.put("getSubject",videoListSubject.getText().toString());
else if(spinner.getSelectedItemPosition()==2)data.put("getSubject",videoSubject.getText().toString());
if(spinner.getSelectedItemPosition()!=0) data.put("getActivity",spinner.getSelectedItem().toString());
data.put("img_url",downloadUri.toString());
jsonObject.put("data",data);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("https://fcm.googleapis.com/fcm/send", jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {

Toast.makeText(NotificationActivity.this, "worked", Toast.LENGTH_SHORT).show();

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(NotificationActivity.this, "failed", Toast.LENGTH_SHORT).show();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {

Map<String, String> params = new HashMap<String, String>();
params.put("Authorization","key=somekey");
params.put("Content-Type","application/json; UTF-8");
return params;
}
};
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(jsonObjectRequest);
} catch (JSONException e) {
e.printStackTrace();
}

} catch (MalformedURLException e) {
e.printStackTrace();
}


}

关于java - 服务器发送请求 firebase java.net.protocolException 在读取响应后无法写入请求正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51391418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com