将图像流上传到WCF Web服务

时间:2018-12-11 10:26:50

标签: android retrofit2

我正在尝试将图像从android上传到WCF Web服务,该请求应该是这样的

{
      "docRequest": {
        "Password": "x",
        "UserName": "x",
        "page_no": 1,
        "docs": {
                  // the image stream
                }
    }
}

我使用翻新版2.4

implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'

这是界面

@Multipart
@POST("saveDoc")
Call<UploadResponse> uploadDoc(@Part ("try") WrapperRequest uploadPhotoRequest);

保存数据的wrapperRequest类

public class WrapperRequest
{

    docRequest docRequest;

    public WrapperRequest(WrapperRequest.docRequest docRequest)
    {
        this.docRequest = docRequest;
    }

    public static class docRequest
    {
        RequestBody docs;
        String UserName, Password;
        int page_no;

        public docRequest(RequestBody docs, String UserName, String Password, int page_no)
        {
            this.docs = docs;
            this.UserName = UserName;
            this.Password = Password;
            this.page_no = page_no;

        }

    }
}

我在哪里提出请求

RequestBody docs = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("image", file.getName(),
                        RequestBody.create(MediaType.parse("image/*"), file)).build();

        WrapperRequest.docRequest docRequest = new WrapperRequest.docRequest(docs, sessionManager.getUsername(), sessionManager.getPassword(), page_no);
        WrapperRequest wrapperRequest = new WrapperRequest(docRequest);

        Call<UploadResponse> call = client.uploadDoc(wrapperRequest);

        call.enqueue(new Callback<UploadResponse>() {
            @Override
            public void onResponse(Call<UploadResponse> call, Response<UploadResponse> response)
            {
                progressDialog.dismiss();
            }

            @Override
            public void onFailure(Call<UploadResponse> call, Throwable t)
            {
                progressDialog.dismiss();
                ShowAlertDialog(t.getMessage());
                Log.v("UploadResponse", t.getMessage());
            }
        });

那是发送的请求

{
  "docRequest": {
    "Password": "x",
    "UserName": "x",
    "docs": {
      "boundary": {
        "data": [
          102,
          98,
          54,
          102,
          99,
          57,
          56,
          48,
          45,
          48,
          49,
          97,
          101,
          45,
          52,
          57,
          98,
          55,
          45,
          97,
          55,
          51,
          49,
          45,
          53,
          99,
          50,
          102,
          56,
          99,
          53,
          100,
          49,
          48,
          54,
          97
        ]
      },
      "contentLength": -1,
      "contentType": {
        "mediaType": "multipart/form-data; boundary=fb6fc980-01ae-49b7-a731-5c2f8c5d106a",
        "subtype": "form-data",
        "type": "multipart"
      },
      "originalType": {
        "mediaType": "multipart/form-data",
        "subtype": "form-data",
        "type": "multipart"
      },
      "parts": [
        {
          "headers": {
            "namesAndValues": [
              "Content-Disposition",
              "form-data; name=\"image\"; filename=\"Capture.PNG\""
            ]
          }
        }
      ]
    },
    "page_no": 2
  }
}

很明显,这是一个糟糕的请求,不符合Web服务预期的请求, 我的问题是我如何仅在“文档”中发送图像流而没有所有不想要的数据

0 个答案:

没有答案