DAY ONE
单利
第一种
public class Student {
private Student(){}//私有
private static Student student= new Student();//创建
public static Student getInstance(){//对外
return student;
}
}
第二种
public class Student {
private Student() {}//私有
private static Student student = null;//创建
public static Student getInstance() {//对外
if (student == null) {//判断
synchronized (Object.class) {//上锁锁
if (student == null) {//二次判断
student = new Student();
}
}
}
return student;
}
}
0khttp
okhttp协议介绍
okhttp是一个第三方类库,用于android中请求网络。
get
public void doget(String url, final MyOkListener myOkListener){
Request request = new Request.Builder()
.url(url)
.get()
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {//子线程
});
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
handler.post(new Runnable() {
});
}
});
}
post
public void dopost(String url, HashMap<String,String> map,final MyOkListener myOkListener){
FormBody.Builder builder = new FormBody.Builder();
Set<Map.Entry<String, String>> entries = map.entrySet();
for (Map.Entry<String, String> entry : entries) {
String key = entry.getKey();
String value = entry.getValue();
builder.add(key,value);
}
FormBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {//子线程
handler.post(new Runnable() {
});
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
handler.post(new Runnable() {
});
}
});
}