【问题标题】:How to send post request with String and byte array parameters如何使用字符串和字节数组参数发送发布请求
【发布时间】:2021-03-06 02:47:47
【问题描述】:

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

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

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

我在 C# 中看到了这个答案:Post byte array to Web API server using HttpClient。我在 Java 中需要类似的东西。

在 Base64 字符串中编码字节数组并不能解决我的问题。

我接受任何可以解决我问题的方法,我不需要专门用凌空发帖请求。

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

【问题讨论】:

    标签: java http post android-volley


    【解决方案1】:

    我找到了解决问题的方法。我在 Base64 String 中编码字节数组是错误的。我认为这行不通,因为我是用凌空抽射来做的,但没有用。

    但我发现了一个带有 JSON 的 post 方法,它确实有效。这是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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-16
      • 1970-01-01
      • 2015-08-23
      相关资源
      最近更新 更多