补订单

RetrofitManager

public class RetrofitManager {

    public static OkHttpClient client=new OkHttpClient.Builder()
            .addInterceptor(new MyInterceptor())
            .build();


    public static ApiService apiService = new Retrofit.Builder()
            .baseUrl("https://www.zhaoapi.cn/")
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(ScalarsConverterFactory.create())
            .client(client)
            .build()
            .create(ApiService.class);

    public static void get(String url, Map<String,String> map, Observer observer){
        apiService.get(url,map)
                  .subscribeOn(Schedulers.io())
                  .observeOn(AndroidSchedulers.mainThread())
                  .subscribe(observer);

    }

    public static void post(String url,Map<String,String> map,Observer observer){
        apiService.post(url, map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }


}

ApiService

public interface ApiService {
    /**
     * GET请求
     * @param url
     * @param map
     * @return
     */
    @GET
    Observable<String> get(@Url String url, @QueryMap Map<String, String> map);

    /**
     * POST请求
     * @param url
     * @param map
     * @return
     */
    @FormUrlEncoded
    @POST
    Observable<String> post(@Url String url, @FieldMap Map<String, String> map);

}

BaseObserver

public abstract class BaseObserver<T> implements Observer<String> {
    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(String s) {
        try {
            Type genType=getClass().getGenericSuperclass();
            Type[] params=((ParameterizedType)genType).getActualTypeArguments();
            Class entityClass=(Class)params[0];
            Gson gson=new Gson();
            T t=(T)gson.fromJson(s,entityClass);
            success(t);
        } catch (JsonSyntaxException e) {
            failure(1001);
            e.printStackTrace();
        }
    }

    @Override
    public void onError(Throwable e) {
        try {
            if(e != null){
                if(e instanceof HttpException){
                    failure(HTTP_ERROR);
                } else if(e instanceof SocketException){
                    failure(NET_WORK_ERROR);
                }else {
                    failure(UNKNOW_ERROR);
                }
            }else {
                failure(UNKNOW_ERROR);
            }
            e.printStackTrace() ;
        } catch (Exception e1) {
            failure(UNKNOW_ERROR);
            e1.printStackTrace();
        }
    }

    @Override
    public void onComplete() {

    }
    /**
     * code
     *  1000 UNKNOW_ERROR 未知错误
     *  1001 json 转化异常  parse error
     *  1002 当前网络不可用     java.net.SocketException: Network is unreachable  超时
     *  1003 服务器不可用 401 402 403 500 502 503 504
     * @param code
     */

    public static final int UNKNOW_ERROR = 1000;
    public static final int JSON_FORMAT_ERROR = 1001;
    public static final int NET_WORK_ERROR = 1002;
    public static final int HTTP_ERROR = 1003;


    public abstract void success(T t);
    public abstract void failure(int code);

    public abstract void onNext(LoginBean bean);
}

MyInterceptor

public class MyInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        if (request.method().equals("GET")) {
            String url = request.url().url().toString();
            url += "&source=android";
            Request newRequset = request.newBuilder().url(url).build();
            return chain.proceed(newRequset);
        } else {
            FormBody formBody = (FormBody) request.body();
            FormBody.Builder builder = new FormBody.Builder();
            for (int i = 0; i < formBody.size(); i++) {
                builder.add(formBody.name(i), formBody.value(i));
            }
            builder.add("source", "android");
            FormBody newFormBody = builder.build();
            Request newRequest = request.newBuilder().url(request.url().url().toString()).post(newFormBody).build();
            return chain.proceed(newRequest);
        }

    }
}

fragment_payed

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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/recyclerView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</FrameLayout>

fragment_wait_pay

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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/recyclerView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</FrameLayout>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值