如何使用字符串和字节数组参数发送 post 请求

How to send post request with String and byte array parameters

我需要在 post 请求中发送一个字节数组。我正在尝试使用 volley,但我只知道如何仅使用 String 参数发送 post。具体我需要在请求中填写以下字段:

    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"));

我知道上面的方法不行。这只是我必须如何填写 post 请求的示例。

我在 C# 中看到了这个答案:。我在 Java.

中需要类似的东西

在 Base64 字符串中编码字节数组没有解决我的问题。

我接受任何可以解决我问题的方法,我不需要专门用 volley 做 post 请求。

PS.: 对不起我的英语不好

我找到了解决问题的方法。我在 Base64 字符串中对字节数组进行编码是错误的。我认为那行不通,因为我是用截击来做的,但没有用。

但我发现了一个 post 方法 JSON 并且它确实有效。这是 post 方法:

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

这是将字节数组编码成字符串的方法:

public String getStringImage(byte[] bm) {
        ByteArrayOutputStream ba = new ByteArrayOutputStream();
        String encode = android.util.Base64.encodeToString(bm, Base64.DEFAULT);
        return encode;
    }