androidokhttpokio

How to make a post request using Okio?


I'm using Okio to download a file....with my request i'm sending some parameters, but since I wasn't getting my file and I was able to log my request and this is what is see:

Why tags is null? that means that the parameters are null

Request: Request{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();

Solution

  • Here is example:

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

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