Android: How to make a POST request to get an image

0

I need to get a Bitmap from the server through a POST method with the body (JSON) and header to put in an ImageView.

I tried using Volley's ImageRequest but I would need to pass the direct URL of my image, which does not happen. I have a URL with some parameters and a body in JSON (which sends the data of the image I'm looking for) and the server would bring me the image if I found it but I'm getting a 405 error.

I tried the same method by PostMan and managed to get the image without problems.

My code:

RequestQueue sQueueFoto = Volley.newRequestQueue(getActivity());
                    //REQUEST FOTO
                    String sEnderecoBaseFoto = Variaveis.WebServicePocket + "/api/Imagem/PegarImagem?redeCodigo=" + Variaveis.Rede + "&largura=120&altura=120";

                    ImageRequest sRequestFoto = new ImageRequest(sEnderecoBaseFoto,
                            new Response.Listener<Bitmap>() {
                                @Override
                                public void onResponse(Bitmap bitmap)
                                {
                                    sProdutos.getDadosProduto().setImagemProduto(bitmap);
                                }
                            }, 0, 0, null,
                            new Response.ErrorListener()
                            {
                                public void onErrorResponse(VolleyError error) {
                                    Mensagem.ExibirAlert(getActivity(), String.valueOf(error.networkResponse.statusCode));
                                }
                            })

                    {
                        @Override
                        public Map<String, String> getHeaders() throws AuthFailureError {
                            Map<String, String> sHeaders = new HashMap<>();
                            sHeaders.put("Authorization", "Bearer " + Variaveis.WebServicePocketToken);
                            return sHeaders;
                        }

                        @Override
                        public byte[] getBody() throws AuthFailureError
                        {
                            return new Gson().toJson(infoImagem).getBytes();
                        }

                        @Override
                        public String getBodyContentType() {
                            return "application/json";
                        }
                    };

                    sQueueFoto.add(sRequestFoto);
    
asked by anonymous 19.04.2017 / 16:33

1 answer

1

I did it that way and it worked correctly:

RequestQueue sQueueFoto = Volley.newRequestQueue(getActivity());

                    String sEnderecoBaseFoto = Variaveis.WebServicePocket + "/api/Imagem/PegarImagem?redeCodigo=" + Variaveis.Rede + "&largura=60&altura=60";

                    final InformacaoImagem infoImagem = sResultado.getDadosProduto().getFoto();

                    Request sRequestFoto = new Request(Request.Method.POST, sEnderecoBaseFoto, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }
                    }) {
                        @Override
                        protected Response parseNetworkResponse(NetworkResponse response) {
                            byte[] sArray = response.data;
                            Bitmap sFoto = BitmapFactory.decodeByteArray(sArray, 0, sArray.length);
                            sProdutos.getDadosProduto().setImagemProduto(sFoto);


                            return Response.success(response, HttpHeaderParser.parseCacheHeaders(response));

                        }

                        @Override
                        protected void deliverResponse(Object response) {

                        }

                        @Override
                        public Map<String, String> getHeaders() throws AuthFailureError {
                            Map<String, String> sHeaders = new HashMap<>();
                            sHeaders.put("Authorization", "Bearer " + Variaveis.WebServicePocketToken);
                            return sHeaders;
                        }

                        @Override
                        public byte[] getBody() throws AuthFailureError {
                            return new Gson().toJson(infoImagem).getBytes();
                        }

                        @Override
                        public String getBodyContentType() {
                            return "application/json";
                        }

                    };
                    sQueueFoto.add(sRequestFoto);
                    sQueueFoto.addRequestFinishedListener(new RequestQueue.RequestFinishedListener<Object>() {
                        @Override
                        public void onRequestFinished(Request<Object> request) {
                            CarregarListView();
                        }
                    });
    
25.04.2017 / 18:57