基本请求展示

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);
    }
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值