OKhttp PUT示例 [英] OKhttp PUT example
问题描述
我的要求是使用PUT
,将标头和正文发送到服务器,这将更新数据库中的某些内容.
My requirement is to use PUT
, send a header and a body to server which will update something in the database.
我刚刚阅读了 okHttp文档,我试图使用他们的POST
示例,但不适用于我的示例用例(我认为可能是因为服务器要求我使用PUT
而不是POST
).
I just read okHttp documentation and I was trying to use their POST
example but it doesn't work for my use case (I think it might be because the server requires me to use PUT
instead of POST
).
这是我使用POST
的方法:
public void postRequestWithHeaderAndBody(String url, String header, String jsonBody) {
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, jsonBody);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.post(body)
.addHeader("Authorization", header)
.build();
makeCall(client, request);
}
我尝试使用PUT
搜索okHttp示例,但没有成功,如果我需要使用PUT
方法,是否仍然可以使用okHttp?
I have tried to search for okHttp example using PUT
with no success, if I need to use PUT
method is there anyway to use okHttp?
我使用的是okhttp:2.4.0(以防万一),谢谢您的帮助!
I'm using okhttp:2.4.0 (just in case), thanks on any help!
推荐答案
用.put
public void putRequestWithHeaderAndBody(String url, String header, String jsonBody) {
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, jsonBody);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.put(body) //PUT
.addHeader("Authorization", header)
.build();
makeCall(client, request);
}
这篇关于OKhttp PUT示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!