首先先导包
这里写代码片
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.google.code.gson:gson:2.8.0'
compile 'com.squareup.okio:okio:1.5.0'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'io.reactivex:rxandroid:1.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
封装
这里写代码片
import android.util.Log;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitUtils {
private static RetrofitUtils retrofitUtils;
private RetrofitUtils(){
}
public static RetrofitUtils getInstance(){
if(retrofitUtils==null){
synchronized (RetrofitUtils.class){
if(retrofitUtils==null){
retrofitUtils=new RetrofitUtils();
}
}
}
return retrofitUtils;
}
private static Retrofit retrofit;
public static synchronized Retrofit getRetrofit(String url){
HttpLoggingInterceptor httpLoggingInterceptor=new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.i("xxx",message);
}
});
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient=new OkHttpClient.Builder()
.addInterceptor(httpLoggingInterceptor)//拦截器
.connectTimeout(5000, TimeUnit.SECONDS)
.readTimeout(5000,TimeUnit.SECONDS)
.retryOnConnectionFailure(false)
.build();
if(retrofit==null){
retrofit=new Retrofit.Builder().baseUrl(url)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
}
return retrofit;
}
public <T>T getApiService(String url,Class<T> cl){
Retrofit retrofit = getRetrofit(url);//得到retrofit
return retrofit.create(cl);//返回的就是网络接口对象
}
Retrofit retrofit = RetrofitUtils.getRetrofit(Api.url);
ApiService apiService = retrofit.create(ApiService.class);
Observable<LeftBean> observable = apiService.getHasParams();
observable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map(new Func1<LeftBean, LeftBean>() {
@Override
public LeftBean call(LeftBean leftBean) {
return leftBean;
}
}).subscribe(new Subscriber<LeftBean>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(LeftBean leftBean) {
iFragment02Model.onSuccess(leftBean);
}
});
图片分割
String images = dataBean.getImages();
String[] imgArr = images.split("!");
Picasso.with(context).load(imgArr[0]).into(myListViewHolder.list_iv);
recyclerview 点击事件
private OnItemListener onItemListener;
public interface OnItemListener{
public void onItemClick(FenleiZuoBean.DatasBean.ClassListBean bean);
}
public void setonItemListen(OnItemListener onItemListener){
this.onItemListener = onItemListener;
}
viewHolder.ll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(onItemListener != null){
onItemListener.onItemClick(list.get(position));
}
}
});
视频播放
先导依赖
compile 'com.github.leifzhang:IjkLib:0.4.3'
布局文件
<tv.danmaku.ijk.media.widget.media.IjkVideoView
android:id="@+id/ijkpayer"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="center"></tv.danmaku.ijk.media.widget.media.IjkVideoView>
代码
IjkMediaPlayer.loadLibrariesOnce(null);
IjkMediaPlayer.native_profileBegin("libijkplayer.so");
AndroidMediaController controller = new AndroidMediaController(getActivity(), false);
mIjkpayer.setMediaController(controller);
String url = "https://wdl.wallstreetcn.com/41aae4d2-390a-48ff-9230-ee865552e72d";
// String url = "http://o6wf52jln.bkt.clouddn.com/演员.mp3";
mIjkpayer.setVideoURI(Uri.parse(url));
mIjkpayer.start();