Okhttp3 - RequestBody.create(contentType, content) Deprecated

后端 未结 6 1953
清酒与你
清酒与你 2020-12-29 17:57

I did not find any example of how to replace the deprecation method. The examples on the okhttp3 main page are old. This is one of them:

public static final          


        
6条回答
  •  -上瘾入骨i
    2020-12-29 18:22

    Just had a quick look at the documentation . It reads deprecated, however the alternative is provided in the doc.
    json.toRequestBody(contentType) should do the trick for you.
    Below is the documentation link:
    https://github.com/square/okhttp/blob/master/okhttp/src/main/java/okhttp3/RequestBody.kt

    public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
    
    OkHttpClient client = new OkHttpClient();
    
    String post(String url, String json) throws IOException {
        RequestBody body = RequestBody.Companion.create(json, JSON)
          Request request = new Request.Builder()
              .url(url)
              .post(body)
              .build();
      try (Response response = client.newCall(request).execute()) {
        return response.body().string();
      }
    }
    

提交回复
热议问题