API
package com.bwie.day1010.net;
public interface Api {
@GET(“product/getProducts”)
Observable getRespBody(@Query(“pscid”) int id);
@POST(“product/getProducts”)
@FormUrlEncoded
Call postRespBody(@Field(“pscid”) int id);
}
Constant
package com.bwie.day1010.net;
public class Constant {
public final static String url=“https://www.zhaoapi.cn/product/”;
}
HttpUtils
package com.bwie.day1010.net;
public class HttpUtils {
public final Api api;
private HttpUtils() {
//日志拦截
OkHttpClient okHttpClient = new OkHttpClient.Builder().addNetworkInterceptor(new LoggingInterceptor()).build();
Retrofit retrofit = new Retrofit.Builder().baseUrl(Constant.url)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build();
api = retrofit.create(Api.class);
}
private static class GetHttpUtilsInstance{
private static HttpUtils httpUtils=new HttpUtils();
}
public static HttpUtils getHttpUtilsInstance(){
return GetHttpUtilsInstance.httpUtils;
}
class LoggingInterceptor implements Interceptor{
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
long t1 = System.nanoTime();
Response response = chain.proceed(request);
long t2 = System.nanoTime();
return response;
Request request = chain.request();
Request.Builder builder = chain.request().newBuilder().addHeader("source","android");
Log.d("xxxxxx","request:"+request);
Response proceed = chain.proceed(request);
return proceed;
}
}
}
Mainactivity
Observable body = HttpUtils.getHttpUtilsInstance().api.getRespBody(1);
body.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(UserInfo userInfo) {
Log.i("aaa", userInfo.getData().get(0).getTitle());
List<UserInfo.DataBean> data = userInfo.getData();
rec.setLayoutManager(new LinearLayoutManager(MainActivity.this));
adapter=new MyAdapter(MainActivity.this,data);
rec.setAdapter(adapter);
}
@Override
public void onError(Throwable e) {
Log.i("aaa", e+"");
}
@Override
public void onComplete() {
}
});
adapter
package com.bwie.day1010.adapter;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private Context context;
private List<UserInfo.DataBean> data;
public MyAdapter(Context context, List<UserInfo.DataBean> data) {
this.context = context;
this.data = data;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.text01.setText(data.get(position).getTitle());
Uri uri = Uri.parse(data.get(position).getImages().split("\\|")[0]);
holder.img.setImageURI(uri);
}
@Override
public int getItemCount() {
return data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final TextView text01;
private final SimpleDraweeView img;
public ViewHolder(View itemView) {
super(itemView);
text01 = itemView.findViewById(R.id.text01);
img = itemView.findViewById(R.id.img);
}
}
}