Retrofi简单使用

所有项目的需要


Retrofi简单使用_第1张图片


XML需要的项目


Retrofi简单使用_第2张图片

依赖



Retrofi简单使用_第3张图片

... java

  importretrofit2.Call

 importretrofit2.http.GET;

importretrofit2.http.Query;

public interfaceIGetDataBean {

//路径

//http://api.tianapi.com/meinv/?key=37e12d2e731527b92aa3276a8fd1ddf4&num=50

@GET("/meinv/")

Call get(@Query("key") String key,@Query("num") String num);

}





,,,java

 importretrofit2.Retrofit;

importretrofit2.converter.gson.GsonConverterFactory;

/**

* Created by DELL on 2017/12/2.

*/

public classIApplicarionextendsApplication {

public staticIGetDataBeaniGetDataBean;

@Override

public voidonCreate() {

super.onCreate();

Fresco.initialize(this);

Retrofit retrofit=newRetrofit.Builder().baseUrl("http://api.tianapi.com")

.addConverterFactory(GsonConverterFactory.create())

.build();

iGetDataBean= retrofit.create(IGetDataBean.class);

}

}

,,,


,,,java

public interfaceModelCallBack {

public voidonSuccess(Bean bean);

public voidonFailure(Exception e);

}

,,,

,,,java


public class MainPresenter {

private IMainView iview;

private MianModelimpl mianModelimpl;

public MainPresenter(IMainView view) {

this.iview = view;

this.mianModelimpl=new MianModelimpl();

}

public  void get(){

mianModelimpl.getdata(new ModelCallBack() {

@Override

public void onSuccess(Bean bean) {

if(iview != null){

iview.onSuccess(bean);

}

}

@Override

public void onFailure(Exception e) {

if(iview != null){

iview.onFailure(e);

}

}

});

}

public void  post(){

mianModelimpl.postdata(new ModelCallBack() {

@Override

public void onSuccess(Bean bean) {

if(iview != null){

iview.onSuccess(bean);

}

}

@Override

public void onFailure(Exception e) {

if(iview != null){

iview.onFailure(e);

}

}

});

}

}

,,,

public class MianModelimpl { public void getdata(final ModelCallBack modelCallBack){ Callcall = IApplication.iGetDataBean.get("37e12d2e731527b92aa3276a8fd1ddf4","50"); call.enqueue(new Callback() { @Override public void onResponse(Callcall, Responseresponse) { System.out.println("response = " + response); Bean bean = response.body(); modelCallBack.onSuccess(bean); IApplication.session.getNewslistBeanDao().insertInTx(bean.getNewslist()); } @Override public void onFailure(Callcall, Throwable t) { modelCallBack.onFailure(new Exception("")); } }); } public void postdata(final ModelCallBack modelCallBack){ HashMaphashMap = new HashMap<>(); hashMap.put("key","37e12d2e731527b92aa3276a8fd1ddf4"); hashMap.put("num","50"); Callcall = IApplication.iGetDataBean.post(hashMap); call.enqueue(new Callback() { @Override public void onResponse(Callcall, Responseresponse) { System.out.println("re = " + response); Bean bean = response.body(); modelCallBack.onSuccess(bean);// IApplication.session.getNewslistBeanDao().insertInTx(bean.getNewslist()); } @Override public void onFailure(Callcall, Throwable t) {

modelCallBack.onFailure(new Exception(""));

}

});

}

}


,,,java

public class IAdapter extends RecyclerView.Adapter{ Context context; Listlist;

public IAdapter(Context context) {

this.context = context;

}

public void  adddata(Bean bean){

if (list==null){

list=new ArrayList<>();

}

list.addAll(bean.getNewslist());

notifyDataSetChanged();

}

@Override

public IAdapter.IViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View view = View.inflate(context, R.layout.layout, null);

return  new IViewHolder(view);

}

@Override

public void onBindViewHolder(IAdapter.IViewHolder holder, int position) {

holder.adapterSdv.setImageURI(list.get(position).getPicUrl());

holder.adapterTextview.setText(list.get(position).getDescription());

}

@Override

public int getItemCount() {

return list==null ? 0 :list.size();

}

static class IViewHolder extends RecyclerView.ViewHolder {

@BindView(R.id.adapter_sdv)

SimpleDraweeView adapterSdv;

@BindView(R.id.adapter_textview)

TextView adapterTextview;

IViewHolder(View view) {

super(view);

ButterKnife.bind(this, view);

}

}

}


,,,java

你可能感兴趣的:(Retrofi简单使用)