【问题标题】:How to make a post request using Okio?如何使用 Okio 发出 post 请求?
【发布时间】:2019-09-28 16:33:21
【问题描述】:

我正在使用 Okio 下载文件....根据我的请求,我正在发送一些参数,但由于我没有得到我的文件并且我能够记录我的请求,这就是所看到的:

为什么标签是空的?这意味着参数为空

请求:请求{method=POST, url=https://mywesite.com/, tag=null}

 RequestBody requestBody = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("user", "test")
                    .addFormDataPart("pass", "1234")
                    .build();
            Request request = new Request.Builder()
                    .url(imageLink)
                    .post(requestBody)
                    .build();

【问题讨论】:

    标签: android okhttp okhttp3 okio


    【解决方案1】:

    示例如下:

    String post(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
            .url(url)
            .post(body)
            .build();
        try (Response response = client.newCall(request).execute()) {
          return response.body().string();
        }
      }
    

    来自:https://github.com/square/okhttp/blob/master/samples/guide/src/main/java/okhttp3/guide/PostExample.java

    【讨论】:

      猜你喜欢
      • 2014-06-20
      • 2016-08-25
      • 2022-11-02
      • 2011-05-11
      • 2019-04-05
      • 2015-09-11
      • 1970-01-01
      • 1970-01-01
      • 2012-07-02
      相关资源
      最近更新 更多