Okhttp封装,结合ImageLoader,
来源:互联网 发布:淘宝托管真的有用吗 编辑:程序博客网 时间:2024/05/17 09:09
布局文件在这里就不声明了,根据自己的需求创建吧!!!
导入OkHttp,Gson依赖
compile 'com.zhy:okhttputils:2.0.0'
compile 'com.google.code.gson:gson:2.8.2'compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'
清单文件声明权限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Acticity的操作
package shizhaoyu1506b20170825.okhttp;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.widget.ImageView;import com.nostra13.universalimageloader.core.ImageLoader;import java.io.IOException;import java.util.List;public class MainActivity extends AppCompatActivity { private ImageView image_pic; private String URL="http://169.254.4.76/mobile/index.php?act=goods&op=goods_list&page=100"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); } private void initData() { HttpUils.getHttpUtils(MainActivity.this).get(URL, ShopBean.class, new ResultData() { @Override public void onSuccess(BaseBean baseBean) throws IOException { ShopBean shopBean = (ShopBean) baseBean; List<ShopBean.DatasBean.GoodsListBean> goods_list = shopBean.getDatas().getGoods_list(); App application = (App) getApplication(); ImageLoader imageLoader = application.setImage(); imageLoader.displayImage(goods_list.get(0).getGoods_image_url(),image_pic); } }); } private void initView() { image_pic = (ImageView) findViewById(R.id.watch_pic); }}
声明接口回调
package shizhaoyu1506b20170825.okhttp;import java.io.IOException;/** * 类用途: * 作者:史曌宇 */public interface ResultData { public void onSuccess(BaseBean baseBean) throws IOException;}
创建App类 完成ImageLoder的初始化
package shizhaoyu1506b20170825.okhttp;import android.app.Application;import android.content.Context;import com.nostra13.universalimageloader.core.DisplayImageOptions;import com.nostra13.universalimageloader.core.ImageLoader;import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;/** * 类用途: * 作者:史曌宇 */public class App extends Application { private Context context; private ImageLoader mImageLoader; @Override public void onCreate() { super.onCreate(); initImageLoader(); } private void initImageLoader() { DisplayImageOptions options=new DisplayImageOptions.Builder() .cacheOnDisk(true) .cacheInMemory(true) .showImageOnLoading(R.mipmap.ic_launcher) .build(); ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this) .defaultDisplayImageOptions(options) .build(); mImageLoader = ImageLoader.getInstance(); mImageLoader.init(configuration); } public ImageLoader setImage(){ return mImageLoader; }}
在清单文件注册
android:name=".App"
创建网络请求类,基于OkHttp框架请求数据
package shizhaoyu1506b20170825.okhttp;import android.app.AlertDialog;import android.content.Context;import android.util.Log;import com.google.gson.Gson;import com.squareup.okhttp.OkHttpClient;import com.squareup.okhttp.Request;import com.zhy.http.okhttp.OkHttpUtils;import com.zhy.http.okhttp.callback.StringCallback;import java.io.IOException;/** * 类用途:封装 HttpUils * 作者:史曌宇 */public class HttpUils { private static volatile HttpUils httputils; private static Context context; private Gson gson; private HttpUils(Context context) { //单例模式,封装构造方法 gson = new Gson(); } public static HttpUils getHttpUtils(Context activity) { //单例模式 if (httputils == null) { synchronized (HttpUils.class) { if (httputils == null) { httputils = new HttpUils(context); } } } return httputils; } //拼接接口请求 public void get(String url, String gtc_id, final Class zz,final ResultData resultData) throws IOException { /** * Get请求 */ StringBuffer sb = new StringBuffer(); sb.append(url);// for (Map.Entry<String, String> entry : params.entrySet()) {// entry.getKey();// entry.getValue();// sb.append(entry.getKey());// sb.append("=");// sb.append(entry.getValue()); sb.append("&"); sb.append("gc_id="); sb.append(gtc_id); String s = sb.toString(); OkHttpUtils.get().url(s).build().execute(new StringCallback() { @Override public void onError(Request request, Exception e) { //异常 Log.d("HttpUtils", "onError"); } @Override public void onResponse(String response) { //成功 Log.d("HttpUtils", response); BaseBean basebean = (BaseBean) gson.fromJson(response, zz); int code = basebean.getCode(); if (code == 200) { if (resultData != null) { try { resultData.onSuccess(basebean); } catch (IOException e) { e.printStackTrace(); } } } else if (code == 400) { new AlertDialog.Builder(context).setTitle("提示") .setMessage("服务器错误请稍后再试").show(); } } }); } //直接请求不拼接接口 public void get(String url, final Class zz, final ResultData resultData) { OkHttpClient okHttpClient=new OkHttpClient();// okHttpClient.setConnectTimeout(20,30); OkHttpUtils.get().url(url).build().execute(new StringCallback() { @Override public void onError(Request request, Exception e) { //异常 Log.e("onError", e.getMessage()); } @Override public void onResponse(String response) { //成功 Log.e("HttpUtils", response); BaseBean basebean = (BaseBean) gson.fromJson(response, zz); int code = basebean.getCode(); if (code == 200) { if (resultData != null) { try { resultData.onSuccess(basebean); } catch (IOException e) { e.printStackTrace(); } } } else if (code == 400) { new AlertDialog.Builder(context).setTitle("提示") .setMessage("服务器错误请稍后再试").show(); } } }); }}
创建Bean类;
注意:总Bean类用BaseBean 其他所有的Bean类都要继承BaseBean Bean类根据Json串去定义,举个列子,根据事例,自己创建吧!!
BaseBean
package shizhaoyu1506b20170825.okhttp;/** * 类用途: * 作者:史曌宇 */public class BaseBean { private int code; public int getCode() { return code; } public void setCode(int code) { this.code = code; }}
子类Bean
太长了,就不放上来了!!!
public class ShopBean extends BaseBean {}
阅读全文
0 0
- Okhttp封装,结合ImageLoader,
- MVP和Retrofit+Rxjava+OkHttp封装结合请求数据
- 网络请求Okhttp封装加单例加拦截器 结合MVP
- 基于OKHTTP的imageloader
- okhttp拦截器+Imageloader
- okhttp封装okhttp-utils
- 封装OKHttp
- 封装OkHttp
- okhttp封装
- 封装OKHttp
- 封装OKHttp
- Okhttp封装
- OkHttp 封装
- OkHttp封装
- OKHttp封装
- Okhttp封装
- okHttp封装
- OkHttp封装
- 使用Glide获取图片缓存大小以及清除缓存
- java_SSM框架浅析
- BZOJ 4517 [Sdoi2016]排列计数 数论
- 2017-9-17pat甲级 C
- 22. Generate Parentheses(生成括号)
- Okhttp封装,结合ImageLoader,
- 当前时间
- 使用Runtime类 识别 sdcard和U盘android 6.0 存储路径识别
- 【数据结构】最短路径——Dijkstra算法
- stata12.13免费破解版网址
- mysql在查询结果列表前添加一列递增的序号列(最简)
- 本地缓存
- 图片圆角
- Oracle数据库入门 基础知识day04 火推阳光笔记