简体   繁体   English

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

[英]Server Send Requests firebase java.net.protocolexception cannot write request body after response has been read

i am trying to send notificaiton using firebase ... the code was working 7 days ago but now all i get is (java.net.protocolexception cannot write request body after response has been read) whenever i call conn.getOutputStream(); 我正在尝试使用Firebase发送通知...代码 7天前就可以使用了,但是现在我所得到的是只要读取 conn.getOutputStream(), java.net.protocolexception就无法在读取响应后写入请求正文 );

thank you guys for help 谢谢你们的帮助

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

i solved it using volley library this is my solution... thanks all 我用排球库解决了这是我的解决方案...谢谢大家

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


    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将文件上传到服务器,java.net.ProtocolException:读取响应后无法写入请求正文 - Upload File To Server, java.net.ProtocolException: cannot write request body after response has been read java.net.ProtocolException:读取输入后无法写入输出 - java.net.ProtocolException: Cannot write output after reading input java.net.ProtocolException: 方法不支持请求正文:GET - java.net.ProtocolException: method does not support a request body: GET java.net.ProtocolException:在哪里读取输入? - java.net.ProtocolException: where is input being read? java.net.ProtocolException: 服务器重定向次数过多 (20) - java.net.ProtocolException: Server redirected too many times (20) java.net.ProtocolException: 服务器重定向次数过多 - java.net.ProtocolException: Server redirected too many times OkHttp“java.net.ProtocolException:意外状态行:nullHTTP / 1.1 200 OK”在使用相同连接的“204 NO-CONTENT”响应之后 - OkHttp “java.net.ProtocolException: Unexpected status line: nullHTTP/1.1 200 OK” after a “204 NO-CONTENT” response using same connection android中带有HttpsURLConnection的java.net.protocolException - java.net.protocolException with HttpsURLConnection in android java.net.Authenticator:java.net.ProtocolException:服务器重定向太多次(20) - java.net.Authenticator : java.net.ProtocolException: Server redirected too many times (20) java.net.ProtocolException:当我通过okhttp上传图像到服务器时,发生了意外的流结束 - java.net.ProtocolException: unexpected end of stream happened when i uploaded a image to server by okhttp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM