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

Asked

Viewed 237 times

0

I need to get a server Bitmap 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 URL directly from my image, which does not occur. I have a URL with some parameters and a JSON body (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);

1 answer

1


I did it this 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();
                        }
                    });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.