Retrofi簡單使用

所有項目的需要



XML需要的項目


依賴



... 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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容