边学边做--社区问答--学习笔记(二)

1.okhttp
Post to a Server–模拟一个post请求

public static final MediaType JSON
    = MediaType.get("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();

String post(String url, String json) throws IOException {
  RequestBody body = RequestBody.create(json, JSON);
  Request request = new Request.Builder()
      .url(url)
      .post(body)
      .build();
  try (Response response = client.newCall(request).execute()) {
    return response.body().string();
  }
}

模拟get方法

OkHttpClient client = new OkHttpClient();

String run(String url) throws IOException {
  Request request = new Request.Builder()
      .url(url)
      .build();

  try (Response response = client.newCall(request).execute()) {
    return response.body().string();
  }
}

两者区别在于post请求需要用到requestbody,赋值给request对象,而get请求则不需要。

2.idea快捷键
ctrl+alt+v 自动补全属性名称
ctrl+alt+n 内联,快速放置原文
ctrl+shift+上下键,整行代码上下移动

3.spring的value注解
在properties里面写好内容,在java类中用@Value注解可以将配置文件中的值注入Java类。

例如:在properties配置文件中有如下一条
github.redirect.uri=http://localhost:8887/callback

则在Java类中可以通过如下注解来注入值
@Value("${github.redirect.uri}")
private String redirectUri;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值