==========================//布局item
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:fresco="http://schemas.android.com/apk/res-auto" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="20dp" > <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/img" android:layout_width="100dp" android:layout_height="100dp" fresco:placeholderImage="@mipmap/ic_launcher" /> <TextView android:id="@+id/tv" android:text="7894562" android:textSize="15dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp"/> </LinearLayout> =====================MM==============、 //接口public interface Imm { public void shuju(Callback callback); }
//类public class Model implements Imm { @Override public void shuju(Callback callback) { OkHttp3Utils.doGet("http://apiv3.yangkeduo.com/v5/newlist?page=1&size=10",callback); } }
========================VV====================public interface Ivv { public void ShowView(Bean bean); }============================PP==================public class Presentor { Context context; Model mm; Ivv vv; public Presentor(Context context, Ivv vv) { this.context = context; this.vv = vv; mm=new Model(); } public void getshuju(){ mm.shuju(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { Gson gson=new Gson(); String www=response.body().string(); Bean bean=gson.fromJson(www,Bean.class); vv.ShowView(bean); } }); } }
===========================Myadapter===============public class Myadapter extends RecyclerView.Adapter<Myadapter.MyViewHolder> { Context context; Bean bean; public Myadapter(Context context, Bean bean) { this.context = context; this.bean = bean; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view=View.inflate(context,R.layout.item,null); MyViewHolder holder=new MyViewHolder(view); return holder; } @Override public void onBindViewHolder(MyViewHolder holder, int position) { holder.tv.setText(bean.getGoods_list().get(position).getGoods_name()); DraweeController controller= Fresco.newDraweeControllerBuilder() .setUri(bean.getGoods_list().get(position).getImage_url()) .setAutoPlayAnimations(true) .build(); holder.img.setController(controller); } @Override public int getItemCount() { return bean.getGoods_list().size(); } class MyViewHolder extends RecyclerView.ViewHolder{ SimpleDraweeView img; TextView tv; public MyViewHolder(View itemView) { super(itemView); img= (SimpleDraweeView) itemView.findViewById(R.id.img); tv= (TextView) itemView.findViewById(R.id.tv); } } }==========================MainAtivity=============public class MainActivity extends AppCompatActivity implements Ivv{ RecyclerView rv; Presentor pp; Myadapter ada; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getSupportActionBar().hide(); Fresco.initialize(this); setContentView(R.layout.activity_main); rv= (RecyclerView) findViewById(R.id.rv); inint(); pp=new Presentor(this,this); pp.getshuju(); } private void inint() { LinearLayoutManager manager=new LinearLayoutManager(this); rv.setLayoutManager(manager); rv.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL)); } @Override public void ShowView(Bean bean) { ada=new Myadapter(this,bean); runOnUiThread(new Runnable() { @Override public void run() { rv.setAdapter(ada); } }); } }
=====================================OkHttp3Utils
public class OkHttp3Utils { private static OkHttpClient okHttpClient = null; public OkHttp3Utils() { } public static OkHttpClient getInstance() { if (okHttpClient == null) { //加同步安全 synchronized (OkHttp3Utils.class) { if (okHttpClient == null) { //判空 为空创建实例 // okHttpClient = new OkHttpClient(); /** * 和OkHttp2.x有区别的是不能通过OkHttpClient直接设置超时时间和缓存了,而是通过OkHttpClient.Builder来设置, * 通过builder配置好OkHttpClient后用builder.build()来返回OkHttpClient, * 所以我们通常不会调用new OkHttpClient()来得到OkHttpClient,而是通过builder.build(): */ // File sdcache = getExternalCacheDir(); File sdcache = new File(Environment.getExternalStorageDirectory(), "cache"); int cacheSize = 10 * 1024 * 1024; okHttpClient = new OkHttpClient.Builder().connectTimeout(15, TimeUnit.SECONDS) .writeTimeout(20, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS).cache(new Cache(sdcache.getAbsoluteFile(), cacheSize)).build(); } } } return okHttpClient; } /** * get请求 * 参数1 url * 参数2 回调Callback */ public static void doGet(String url, Callback callback) { //创建OkHttpClient请求对象 OkHttpClient okHttpClient = getInstance(); //创建Request Request request = new Request.Builder().url(url).build(); //得到Call对象 Call call = okHttpClient.newCall(request); //执行异步请求 call.enqueue(callback); } /** * post请求 * 参数1 url * 参数2 回调Callback */ public static void doPost(String url, Map<String, String> params, Callback callback) { //创建OkHttpClient请求对象 OkHttpClient okHttpClient = getInstance(); //3.x版本post请求换成FormBody 封装键值对参数 FormBody.Builder builder = new FormBody.Builder(); //遍历集合 for (String key : params.keySet()) { builder.add(key, params.get(key)); } //创建Request Request request = new Request.Builder().url(url).post(builder.build()).build(); Call call = okHttpClient.newCall(request); call.enqueue(callback); } /** * post请求上传文件 * 参数1 url * 参数2 回调Callback */ public static void uploadPic(String url, File file, String fileName) { //创建OkHttpClient请求对象 OkHttpClient okHttpClient = getInstance(); //创建RequestBody 封装file参数 RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file); //创建RequestBody 设置类型等 RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", fileName, fileBody).build(); //创建Request Request request = new Request.Builder().url(url).post(requestBody).build(); //得到Call Call call = okHttpClient.newCall(request); //执行请求 call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { //上传成功回调 目前不需要处理 } }); } /** * Post请求发送JSON数据 * 参数一:请求Url * 参数二:请求的JSON * 参数三:请求回调 */ public static void doPostJson(String url, String jsonParams, Callback callback) { RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonParams); Request request = new Request.Builder().url(url).post(requestBody).build(); Call call = getInstance().newCall(request); call.enqueue(callback); } /** * 下载文件 以流的形式把apk写入的指定文件 得到file后进行安装 * 参数一:请求Url * 参数二:保存文件的路径名 * 参数三:保存文件的文件名 */ public static void download(final MainActivity context, final String url, final String saveDir) { Request request = new Request.Builder().url(url).build(); Call call = getInstance().newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.i("xxx", e.toString()); } @Override public void onResponse(Call call, final Response response) throws IOException { InputStream is = null; byte[] buf = new byte[2048]; int len = 0; FileOutputStream fos = null; try { is = response.body().byteStream(); //apk保存路径 final String fileDir = isExistDir(saveDir); //文件 File file = new File(fileDir, getNameFromUrl(url)); context.runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(context, "下载成功:" + fileDir + "," + getNameFromUrl(url), Toast.LENGTH_SHORT).show(); } }); fos = new FileOutputStream(file); while ((len = is.read(buf)) != -1) { fos.write(buf, 0, len); } fos.flush(); //apk下载完成后 调用系统的安装方法 Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); context.startActivity(intent); } catch (IOException e) { e.printStackTrace(); } finally { if (is != null) is.close(); if (fos != null) fos.close(); } } }); } /** * @param saveDir * @return * @throws IOException 判断下载目录是否存在 */ public static String isExistDir(String saveDir) throws IOException { // 下载位置 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File downloadFile = new File(Environment.getExternalStorageDirectory(), saveDir); if (!downloadFile.mkdirs()) { downloadFile.createNewFile(); } String savePath = downloadFile.getAbsolutePath(); Log.e("savePath", savePath); return savePath; } return null; } /** * @param url * @return 从下载连接中解析出文件名 */ private static String getNameFromUrl(String url) { return url.substring(url.lastIndexOf("/") + 1); } }