【问题标题】:Server Send Requests firebase java.net.protocolexception cannot write request body after response has been read服务器发送请求 firebase java.net.protocolexception 在读取响应后无法写入请求正文
【发布时间】:2018-07-17 23:16:20
【问题描述】:

我正在尝试使用 firebase 发送通知...代码 在 7 天前正在工作,但现在我得到的只是 (java.net.protocolexception 无法在响应后写入请求正文已阅读) 每当我调用 conn.getOutputStream();

谢谢大家帮忙

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();
                }

【问题讨论】:

  • 您可能必须使用调试器逐步完成此操作,并检查您的 HttpUrlConnection 是否开始未连接,并查看在到达 getOutputStream() 之前是哪一步发出请求,尽管我不能在这里看到任何明显的东西。在构建 JSON 之前尝试 getOutputStream 可能也是值得的,但我不明白为什么这很重要。你的 'UTF-8' 也应该是 'charset=utf-8',但这也不重要。这也是一个 HttpsUrlConnection,但同样不重要。

标签: java android firebase firebase-cloud-messaging httpconnection


【解决方案1】:

我使用 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();
        }


    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    相关资源
    最近更新 更多