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方式提交分块请求
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 logo")) .addPart( headers.of("Content-disposition","form-data; name=\"image\""),Requestbody.create(MEDIA_TYPE_PNG,new file("website/static/logo-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请求的方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)