Android:okhttp发送请求并且添加token至header头(post+get)

拦截器没搞懂就先搞这个法子暂时先写到请求里面,不然api接口啥都用不了,其实也并不是特别麻烦,后续要改,不考虑使用拦截器的话,可能要使用到sqlite数据库,emmmm,想想还是别这么干

 public void postTest(){
        OkHttpClient client = new OkHttpClient();
        //post请求
        FormBody formBody = new FormBody.Builder()
                .add("event_id","33")
                .build();

        Request request = new Request.Builder().url(DecryptionAddress+"app/event/accept").
                addHeader("Token","服务器获取的token").post(formBody).build();

        client.newCall(request).enqueue(new Callback() {
            public void onFailure(Call call, IOException e) {
                System.out.println(e.getMessage());
            }

            public void onResponse(Call call, Response response) throws IOException {
                if(response.code() >= 200 && response.code() < 300) {
                   String result = response.body().string();
                    System.out.println(result);
                }
            }
        });
    }
    public void getTest(){
        //get请求
        OkHttpClient client = new OkHttpClient();
        Request request1 = new Request.Builder()
                .url(DecryptionAddress+"/app/event/detail?event_id=89")
                .addHeader("Token","服务器获取的token")
                .build();

        client.newCall(request1).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println(e.getMessage());
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.code() >= 200 && response.code() < 300) {
                    String result = response.body().string();
                    System.out.println(result);
                }
            }
        });
    }
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Android中使用OkHttp3进行POST和GET请求发送数据到OneNet物联网云平台可以按照以下步骤进行。 首先,要使用OkHttp3,需要将其添加为项目的依赖项。可以在项目的build.gradle文件中添加以下代码来导入OkHttp3库: ```groovy implementation 'com.squareup.okhttp3:okhttp:3.12.1' ``` 接下来,要发送POST请求,可以使用OkHttp3的RequestRequestBody类来构建请求,并使用OkHttpClient来执行请求。 ```java OkHttpClient client = new OkHttpClient(); // 构建请求体 FormBody requestBody = new FormBody.Builder() .add("key1", "value1") .add("key2", "value2") .build(); // 构建请求 Request request = new Request.Builder() .url("https://api.heclouds.com/devices/{deviceId}/datapoints") .addHeader("api-key", "YOUR_API_KEY") .post(requestBody) .build(); // 执行请求 try { Response response = client.newCall(request).execute(); if (response.isSuccessful()) { // 请求成功 String responseData = response.body().string(); // 处理返回的数据 } else { // 请求失败 // 处理错误信息 } } catch (IOException e) { e.printStackTrace(); } ``` 上面的代码示例中,我们创建了一个FormBody作为请求体,并填充了要发送的数据。请求URL需要替换为OneNet物联网云平台实际的URL,同时需要在请求添加API密钥。 对于GET请求,可以使用OkHttp3的Url类来构建请求,同样使用OkHttpClient来执行请求。 ```java OkHttpClient client = new OkHttpClient(); // 构建请求 HttpUrl.Builder urlBuilder = HttpUrl.parse("https://api.heclouds.com/devices/{deviceId}/datapoints").newBuilder(); urlBuilder.addQueryParameter("key1", "value1"); urlBuilder.addQueryParameter("key2", "value2"); // 执行请求 try { Request request = new Request.Builder() .url(urlBuilder.build()) .addHeader("api-key", "YOUR_API_KEY") .get() .build(); Response response = client.newCall(request).execute(); if (response.isSuccessful()) { // 请求成功 String responseData = response.body().string(); // 处理返回的数据 } else { // 请求失败 // 处理错误信息 } } catch (IOException e) { e.printStackTrace(); } ``` 同样地,GET请求URL需要替换为OneNet物联网云平台实际的URL,同时在请求添加API密钥。GET请求需要使用Url类来构建URL,并通过addQueryParameter()方法添加查询参数。 以上就是使用OkHttp3在Android发送POST和GET请求到OneNet物联网云平台的简要步骤。根据实际的接口定义和需求,可能需要调整请求的参数和处理返回数据的逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wenlong Yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值