Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了详解Android中使用OkHttp发送HTTP的post请求的方法大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

http POST 和 PUT 请求可以包含要提交的内容。只需要在创建 request 对象时,通过 post 和 put 方法来指定要提交的内容即可。
http POST 请求的基本示例:

public class PostString {
  public static void main(String[] args) throws IOException {
  OkhttpClient client = new OkhttpClient();
  MediaType MEDIA_TYPE_TEXT = MediaType.parse("text/plain");
  String postBody = "Hello World";

  request request = new request.builder()
      .url("http://www.baidu.com")
      .post(requestBody.create(MEDIA_TYPE_TEXT,postBody))
      .build();

  Response response = client.newCall(request).execute();
  if (!response.issuccessful()) {
    throw new IOException("服务器端错误: " + responsE);
  }

  System.out.println(response.body().String());
  }
}

String 类型提交内容只适用于内容比较小的情况。当请求内容较大时,应该使用流来提交。

Post方式提交流

以流的方式POST提交请求体。请求体的内容由流写入产生。这个例子是流直接写入 Okio 的BufferedSink。你的程序可能会使用 OutputStream ,你可以使用 BufferedSink.outputStream() 来获取

public static final MediaType MEDIA_TYPE_MARKDOWN
 = MediaType.parse("text/x-markdown; charset=utf-8");

private final OkhttpClient client = new OkhttpClient();

public void run() throws Exception {
 requestBody requestBody = new requestBody() {
  @Override public MediaType contentType() {
   return MEDIA_TYPE_MARKDOWN;
  }

  @Override public void writeTo(BufferedSink sink) throws IOException {
   sink.writeUtf8("numbers\n");
   sink.writeUtf8("-------\n");
   for (int i = 2; i <= 997; i++) {
  sink.writeUtf8(String.format(" * %s = %s\n",i,factor(i)));
   }
  }

  private String factor(int n) {
   for (int i = 2; i < n; i++) {
  int x = n / i;
  if (x * i == n) return factor(X) + " × " + i;
   }
   return Integer.toString(n);
  }
 };

 request request = new request.builder()
   .url("https://api.github.com/markdown/raw")
   .post(requestBody)
   .build();

 Response response = client.newCall(request).execute();
 if (!response.issuccessful()) throw new IOException("Unexpected code " + responsE);

 System.out.println(response.body().String());
}

Post方式提交文件

文件作为请求体是十分简单的。

public static final MediaType MEDIA_TYPE_MARKDOWN
 = MediaType.parse("text/x-markdown; charset=utf-8");

private final OkhttpClient client = new OkhttpClient();

public void run() throws Exception {
 File file = new File("README.md");

 request request = new request.builder()
   .url("https://api.github.com/markdown/raw")
   .post(requestBody.create(MEDIA_TYPE_MARKDOWN,filE))
   .build();

 Response response = client.newCall(request).execute();
 if (!response.issuccessful()) throw new IOException("Unexpected code " + responsE);

 System.out.println(response.body().String());
}

Post方式提交表单

使用 FormEncodingBuilder 来构建和HTML <form> 标签相同效果的请求体。键值对将使用一种HTML兼容形式的URL编码来进行编码。

private final OkhttpClient client = new OkhttpClient();

public void run() throws Exception {
 requestBody formBody = new FormEncodingBuilder()
   .add("search","Jurassic Park")
   .build();
 request request = new request.builder()
   .url("https://en.wikipedia.org/w/index.PHP")
   .post(formBody)
   .build();

 Response response = client.newCall(request).execute();
 if (!response.issuccessful()) throw new IOException("Unexpected code " + responsE);

 System.out.println(response.body().String());
}

Post方式提交分块请求

@H_917_0@multipartBuilder 可以构建复杂的请求体,与HTML文件上传形式兼容。多块请求体中每块请求都是一个请求体,可以定义自己的请求头。这些请求头可以用来描述这块请求,例如他的 Content-Disposition 。如果 Content-Length 和 Content-Type 可用的话,他们会被自动添加到请求头中。

private static final String imgur_CLIENT_ID = "...";
private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");

private final OkhttpClient client = new OkhttpClient();

public void run() throws Exception {
 // Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image
 requestBody requestBody = new MultipartBuilder()
   .type(MultipartBuilder.FORM)
   .addPart(
     Headers.of("Content-Disposition","form-data; name=\"title\""),requestBody.create(null,"Square @L_197_27@"))
   .addPart(
     Headers.of("Content-Disposition","form-data; name=\"image\""),requestBody.create(MEDIA_TYPE_PNG,new File("website/static/@L_197_27@-square.png")))
   .build();

 request request = new request.builder()
   .header("Authorization","Client-ID " + imgur_CLIENT_ID)
   .url("https://api.imgur.com/3/image")
   .post(requestBody)
   .build();

 Response response = client.newCall(request).execute();
 if (!response.issuccessful()) throw new IOException("Unexpected code " + responsE);

 System.out.println(response.body().String());
}

大佬总结

以上是大佬教程为你收集整理的详解Android中使用OkHttp发送HTTP的post请求的方法全部内容,希望文章能够帮你解决详解Android中使用OkHttp发送HTTP的post请求的方法所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: