首先网络权限<uses-permission android:name="android.permission.INTERNET"/>
其次导入依赖包
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' compile 'com.google.code.gson:gson:2.2.4' compile 'com.facebook.fresco:fresco:0.11.0' compile 'io.reactivex:rxjava:1.0.14' compile 'io.reactivex:rxandroid:1.0.1' compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4' compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4' compile 'com.squareup.okhttp3:okhttp:3.9.0' compile 'com.jakewharton:butterknife:8.5.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1' compile 'com.android.support:recyclerview-v7:26.1.0' compile 'com.github.bumptech.glide:glide:3.7.0' }
adapter
package com.example.xie_20171205.adapter; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.example.xie_20171205.R; import com.example.xie_20171205.bean.Bean; import com.facebook.drawee.backends.pipeline.Fresco; import com.facebook.drawee.interfaces.DraweeController; import com.facebook.drawee.view.SimpleDraweeView; 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.tv1.setText(bean.getNewslist().get(position).getTitle()); holder.tv2.setText(bean.getNewslist().get(position).getCtime()); DraweeController dc = Fresco.newDraweeControllerBuilder() .setUri(bean.getNewslist().get(position).getPicUrl()) .setAutoPlayAnimations(true) .build(); holder.img.setController(dc); } @Override public int getItemCount() { return bean.getNewslist().size(); } class MyViewHolder extends RecyclerView.ViewHolder { private final SimpleDraweeView img; private final TextView tv1,tv2; public MyViewHolder(View itemView) { super(itemView); img = itemView.findViewById(R.id.img); tv1 = itemView.findViewById(R.id.tv1); tv2 = itemView.findViewById(R.id.tv2); } } }
Bean
http://api.tianapi.com/nba/?key=71e58b5b2f930eaf1f937407acde08fe&num=10
model——Imodel
package com.example.xie_20171205.model; import com.example.xie_20171205.bean.Bean; import rx.Observer; public interface Imodel { //将观察者传(bean)进去 public void shuju(int num,Observer<Bean> observer); }
model——Modelpackage com.example.xie_20171205.model; import com.example.xie_20171205.GetRequest_In; import com.example.xie_20171205.bean.Bean; import okhttp3.OkHttpClient; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory; import retrofit2.converter.gson.GsonConverterFactory; import rx.Observable; import rx.Observer; import rx.Subscriber; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; public class Model implements Imodel{ private Bean bean; @Override public void shuju(final int num, Observer<Bean> observer) { //创建被观察者 Observable.create(new Observable.OnSubscribe<Bean>() { @Override public void call(final Subscriber<? super Bean> subscriber) { //拦截器的使用 OkHttpClient oc=new OkHttpClient.Builder().build(); //Retrofit的请求数据 Retrofit retrofit=new Retrofit.Builder() //添加拦截器 .client(oc) //设置网络请求的url .baseUrl("http://api.tianapi.com/nba/") //设置Gson .addConverterFactory(GsonConverterFactory.create()) //设置Rxjava .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build(); //创建网络请求接口的实例 GetRequest_In Request=retrofit.create(GetRequest_In.class); //对发的请求进行封装 Call<Bean> call=Request.getCall(num); //使用异步发送网络请求 call.enqueue(new Callback<Bean>(){ //请求成功时回调 @Override public void onResponse(Call<Bean> call, Response<Bean> response) { bean=response.body(); //调用OnNext方法把值传给观察者 subscriber.onNext(bean); subscriber.onCompleted(); } //请求失败时的回调 @Override public void onFailure(Call<Bean> call, Throwable t) { } }); } }).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()) .subscribe(observer); } }
persenter——Persenterpackage com.example.xie_20171205.persenter; import android.content.Context; import com.example.xie_20171205.bean.Bean; import com.example.xie_20171205.model.Model; import com.example.xie_20171205.view.Iview; import rx.Observer; public class Persenter { //定义属性 Context context; Model mm; Iview vv; public Persenter(Context context, Iview vv) { this.context = context; this.vv = vv; mm=new Model(); } //创建方法 public void getData(){ int num=vv.getNum(); mm.shuju(num,new Observer<Bean>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } //将bean的值让V层接受 @Override public void onNext(Bean bean) { vv.ShowView(bean); } }); } }
view——Iviewpackage com.example.xie_20171205.view; import com.example.xie_20171205.bean.Bean; public interface Iview { //方法有参bean public void ShowView(Bean bean); public int getNum(); }
GetRequest_In
package com.example.xie_20171205; import com.example.xie_20171205.bean.Bean; import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.Query; public interface GetRequest_In { @GET("?key=71e58b5b2f930eaf1f937407acde08fe&num=10") Call<Bean> getCall(@Query("num") int num); }
MainActivitypackage com.example.xie_20171205; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import com.example.xie_20171205.adapter.Myadapter; import com.example.xie_20171205.bean.Bean; import com.example.xie_20171205.persenter.Persenter; import com.example.xie_20171205.view.Iview; import com.facebook.drawee.backends.pipeline.Fresco; import butterknife.BindView; import butterknife.ButterKnife; public class MainActivity extends AppCompatActivity implements Iview { @BindView(R.id.rv) RecyclerView rv; int num=10; Persenter pp; Myadapter rvadapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Fresco.initialize(this); setContentView(R.layout.activity_main); ButterKnife.bind(this); pp=new Persenter(this,this); pp.getData(); } @Override public void ShowView(Bean bean) { LinearLayoutManager manager=new LinearLayoutManager(this); rv.setLayoutManager(manager); rv.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL)); rvadapter=new Myadapter(this,bean); rv.setAdapter(rvadapter); } @Override public int getNum() { return num; } }
OkHttp3Utils
package com.example.xie_20171205;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
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();
// 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;
}
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);
}
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);
}
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 {
//上传成功回调 目前不需要处理
}
});
}
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);
}
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();
}
}
});
}
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;
}
private static String getNameFromUrl(String url) {
return url.substring(url.lastIndexOf("/") + 1);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:fresco="http://schemas.android.com/apk/res-auto">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/img"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
fresco:placeholderImage="@mipmap/ic_launcher"
fresco:roundAsCircle="true" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv2"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/tv2"
android:layout_marginTop="15dp"
android:text="26565465465446"
android:textColor="#ccc"
android:textSize="25dp" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv1"
android:layout_marginLeft="31dp"
android:layout_marginStart="31dp"
android:layout_marginTop="14dp"
android:layout_toEndOf="@+id/img"
android:layout_toRightOf="@+id/img"
android:text="7896546545646"
android:textColor="#f00" />
<TextView
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_alignBottom="@+id/img"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@drawable/fengexian" />
</RelativeLayout>