OKhttp PUT示例 [英] OKhttp PUT example

查看:962
本文介绍了OKhttp PUT示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是使用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 PUTwith no success, if I need to use PUTmethod 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆