okhttp3 使用示例 记录:
MediaType contentType = MediaType.parse("application/json; charset=utf-8");
okhttp3.RequestBody requestBody = okhttp3.RequestBody.create(contentType, new Gson().toJson(str));
HttpUrl url = new HttpUrl.Builder()
请求方式 .scheme("https")
请求地址 .host("api.weixin.qq.com")
地址后面的路径 .addPathSegment("cgi-bin/menu/create")
路径后面的参数 .setQueryParameter("access_token", getAccessToken())
.build();
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
Response response = OkHttp.newCall(request).execute();
if (response.isSuccessful()) {
result = jsresponse.body().string();
} else {
System.out.println("请求异常");
}
--最简单、最粗暴、单线程、多线程、最安全的单例模式 饿汉式
public class A {
private static A a =new A();
private A(){}
public static A getInstance(){
return a;
}
}
3333333333333333
// 线程安全的懒汉式单例
public class Singleton3 {
//使用volatile关键字防止重排序,因为 new Instance()是一个非原子操作,可能创建一个不完整的实例
private static volatile Singleton3 singleton3;
private Singleton3() {
}
public static Singleton3 getSingleton3() {
// Double-Check idiom
if (singleton3 == null) {
synchronized (Singleton3.class) { // 1
// 只需在第一次创建实例时才同步
if (singleton3 == null) { // 2
singleton3 = new Singleton3(); // 3
}
}
}
return singleton3;
}
}/* Output(完全一致):