MVP + Retrofit

1.创建V,P

public interface IView {
    void getHttpData(String data);
}

public interface IPresenter {

    void getHttpInstance(String url);
}

2.分别实现P接口

public class IPresenterImpl implements IPresenter {
    private IView mView;
    private List mIViews = new ArrayList<>();
    public IPresenterImpl(IView view) {
        mView = view;

    }
    public synchronized void registerListener(IView view){
        mIViews.add(view);
        Log.d("wwj", "registerListener: ");
    }


    @Override
    public void getHttpInstance(String url) {
        ApiManger instance = ApiManger.getInstance();
        TngouService tngouService = instance.getTngouService();
        Call listById = tngouService.getListById("1");
        listById.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                GalleryList body = response.body();
                boolean status = body.isStatus();
                List galleries = body.getmGalleries();
                Log.d("wwj", "onResponse: " + status);

                mView.getHttpData("success");
            }

            @Override
            public void onFailure(Call call, Throwable t) {
                String message = t.getMessage();
                Log.d("wwj", "onFailure: "+ message);
            }
        });
    }
    public synchronized void unregisterListener(IView view){
        mIViews.remove(view);
        Log.d("wwj", "unregisterListener: " );
    }
}

3.创建ApiManger管理器

public class ApiManger {

    private static String URL ="http://www.tngou.net";
    private final TngouService mTngouService;

    private  boolean CookieTag = false;
    public ApiManger() {
        Retrofit.Builder builder = new Retrofit.Builder();
        builder.baseUrl(URL).
                addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create());
        if (CookieTag){
            builder.client(new OkHttpClient());
        }
        mTngouService = builder.build().create(TngouService.class);

    }
    private static class ApiMangerHandler{
        private static final ApiManger INSTANCE = new ApiManger();
    }
    public static ApiManger getInstance(){
        return ApiMangerHandler.INSTANCE;
    }
    public  TngouService getTngouService(){
        return ApiManger.getInstance().mTngouService;
    }
    private void setCookieTag(boolean cookieTag){
        CookieTag = cookieTag;
    }
}

4,.创建HttpApi

public interface TngouService {
    //方法 -> 接口请求
    // Call 实际上是网络请求的工具,可以
    /**
     * 定义一个方法,对某个请求网址
     */
    @GET("tnfs/api/classify")
    Call getGalleryClassify();

    /**
     * 定义网址,并且指定GET请求的查询参数 为 id =xxx;
     * @param id
     * @return
     */
    @GET("tnfs/api/list")
    Call getListById(@Query("id") String id);
}
5.主函数调用

 mPresenter = new IPresenterImpl(this);
        mPresenter.registerListener(this);
        mPresenter.getHttpInstance(url);



你可能感兴趣的:(android)