——————写一下网络工具封装类的ICallBack
public interface ICallBack {
void onSuccess(Object obj);
void onFailed(Exception e);
}
——————然后就是那个HttpUtils
public class HttpUtils {
private static volatile HttpUtils instance;
private static final HttpUtils ourInstance = new HttpUtils();
private final OkHttpClient client;
private Handler handler = new Handler();
public static HttpUtils getInstance() {
if (instance == null){
synchronized (HttpUtils.class){
if (null == instance){
instance = new HttpUtils();
}
}
}
return instance;
};
private HttpUtils() {
client = new OkHttpClient();
}
public void get(String url, final ICallBack callback, final Type type){
Request request = new Request
.Builder()
.get()
.url(url)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
callback.onFailed(e);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String result = response.body().string();
Gson gson = new Gson();
final Object o = gson.fromJson(result, type);
handler.post(new Runnable() {
@Override
public void run() {
callback.onSuccess(o);
}
});
}
});
}
}
——————接下来就是解析图片的封装类
public class StringUtils {
public static String https2Http(String url) {
return url.replace("https", "http");
}
}