OKHTTP Android将POST请求作为带图像的表单数据发送

qgelzfjb  于 2022-12-09  发布在  Android
关注(0)|答案(3)|浏览(231)

我有一个API POST请求,它同时接收文本(字符串)和1个图像文件的表单/数据。在postman中,它看起来是这样的,而且工作得很完美:)

我正在尝试通过OKHTTP在Android应用程序上通过POST请求发送相同的内容。以下是我编写的代码:

MediaType mediaType = MediaType.parse(getMimeType(imageFile.toURI().toURL().toString()));
        requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("imageFile", imageFile.getName(), RequestBody.create(imageFile, mediaType))
                .addFormDataPart("machineKey", machineKey)
                .addFormDataPart("authToken", authToken)
                .addFormDataPart("UIID", UIID)
                .addFormDataPart("localItemID", localItemID)
                .addFormDataPart("itemName", itemName)
                .addFormDataPart("itemDescription", itemDescription)
                .addFormDataPart("itemPrice", itemPrice)
                .addFormDataPart("itemStock", itemStock)
                .addFormDataPart("itemAge", itemAge)
                .build();
    Request request = new Request.Builder()
            .url(URLString)
            .post(requestBody)
            .build();
    System.out.println("POST: calling: " +URLString);
    Response response = client.newCall(request).execute();

这里有几个注意事项要记住:)

  1. imageFile是映像的文件(该文件保证始终存在,并且可访问)
    1.所有字符串从不为空
    (getMimeType(imageFile.toURI().toURL().toString())将返回"image/jpeg""image/*",我已尝试了这两种方法
    我正在异步运行这段代码,这段代码将正确地发布字符串值,但由于某些原因,将无法正确地发布图像文件。有什么想法吗?谢谢!
o2gm4chl

o2gm4chl1#

实际上想通了...原来我发送的文件名没有经过消毒,所以一些正在发送的文件在其文件名中有非法字符,所以我得到了修复!谢谢!:)

clj7thdc

clj7thdc2#

缺少设置内容类型请求标头的内容:.addHeader("content-type", "multipart/form-data")

m1m5dgzv

m1m5dgzv3#

我正在使用以下代码发送带有访问令牌的表单数据

文件夹文件夹文件夹文件夹文件夹文件夹文件夹文件夹

File pdfFile = new File(directoryPath +"ImagesToPDF.pdf");


                        OkHttpClient client = new OkHttpClient().newBuilder().build();

                        MediaType mediaType = MediaType.parse("text/plain");

                        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                                .addFormDataPart("UploadFile",pdfFile.toString(),
                                        RequestBody.create(MediaType.parse("application/octet-stream"),
                                                new File(pdfFile.toString())))
                                .build();
                        Request request = new Request.Builder()
                                .url("http://ip:port/V1/User/UserUploadingFile")
                                .method("POST", body)
                                .addHeader("Authorization", "Bearer "+token)
                                .build();
                        Response response = null;
                        Call call = client.newCall(request);

                        call.enqueue(new Callback() {
                            @Override
                            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {

                                String responsee = response.body().string();

                                Log.e("response ", "onResponse(): " + responsee );
                                int code=  response.code();

                                if (code == 200){
                                }else if(code == 401){
                                }else if(code == 404){

                                    Intent intent = new Intent(context,LoginActivity.class);
                                    startActivity(intent);
                                    finish();
                                }
                            }

                            @Override
                            public void onFailure(@NonNull Call call, @NonNull IOException e) {

                                Log.e("HttpService", "onFailure() Request was: " + request);

                                e.printStackTrace();
                            }

                        });

相关问题