0

I need to send a byte array in a post request. I'm trying using volley, but I only know how to send post with only String parameters. Specifically I need to fill the following fields in the request:

    params.add(new BasicNameValuePair("Id", ""));
    params.add(new BasicNameValuePair("Empresa", "1"));
    params.add(new BasicNameValuePair("Matricula", "1"));
    params.add(new BasicNameValuePair("Foto", bytearray[] here));  << this is the field that need byte array
    params.add(new BasicNameValuePair("Data", "11/22/2020 16:30:00"));
    params.add(new BasicNameValuePair("GPS", "0"));
    params.add(new BasicNameValuePair("idDispositivo", "0"));
    params.add(new BasicNameValuePair("arquivo", ""));
    params.add(new BasicNameValuePair("face", "0"));
    params.add(new BasicNameValuePair("ip", "0.0"));

I know that the method above doesn't work. It's just an example of how I have to fill the post request.

I saw this answer in C#: Post byte array to Web API server using HttpClient. I need something like that in Java.

Encoding the byte array in Base64 String doesn't solve my problem.

I accept any method that can solve my problem, I don't need specifically do the post request with volley.

PS.: Sorry for my bad english.

1 Answer 1

0

I found a solution to my problem. I was wrong about encoding the byte array in Base64 String. I thought that doesn't work because I was doing that with volley and did not work.

But I found a post method with JSON and it actually works. Here's the post method:

public void novoPost(String empresa, String matricula, byte[] foto, String data, String face) throws IOException {
        URL url = new URL("http://myurl.com");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json; utf-8");
        con.setRequestProperty("Accept", "application/json");
        con.setDoOutput(true);

        String jsonInputString = "{\"Id\":" + "\"" + "" +  "\"" +  "," +
                "\"Empresa\":" + "\"" + empresa +  "\"" + "," +
                "\"Matricula\":" + "\"" + matricula +  "\"" + "," +
                "\"Foto\":" + "\"" + getStringImage(foto) +  "\"" + "," +
                "\"Data\":" + "\"" + data +  "\"" + "," +
                "\"GPS\":" + "\"" + getGPS() +  "\"" + "," +
                "\"idDispositivo\":" + "\"" + getIMEI() +  "\"" + "," +
                "\"arquivo\":" + "\"" + "" +  "\"" + "," +
                "\"face\":" + "\"" + face +  "\"" + "," +
                "\"ip\":" + "\"" + getIPAddress(true) +  "\"" + "," +
                "}";

        try (OutputStream os = con.getOutputStream()) {
            byte[] input = jsonInputString.getBytes("utf-8");
            os.write(input, 0, input.length);
        }

        int code = con.getResponseCode();
        System.out.println(code);

        try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))) {
            StringBuilder response = new StringBuilder();
            String responseLine = null;
            while ((responseLine = br.readLine()) != null) {
                response.append(responseLine.trim());
            }
            System.out.println(response.toString());
        }
    }

and this is the method that encode the byte array into String:

public String getStringImage(byte[] bm) {
        ByteArrayOutputStream ba = new ByteArrayOutputStream();
        String encode = android.util.Base64.encodeToString(bm, Base64.DEFAULT);
        return encode;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.