我已经为一个按钮创建了改装回调.现在我在创建另一个按钮以从另一个API获取数据时遇到了一些麻烦.
这是我的界面:
public interface APIService { @GET("/API/partners.Json") Call<List<Partner>> getPartners(); @GET("/API/drivers.Json") Call<List<Driver>> getDrivers(); @GET("/API/warehouses.Json") Call<List<Warehouse>> getWarehuses();}
这是我的API助手类:
public class APIHelper { public static final String BASE_URL = "https://part-of-url.herokuapp.com/"; public static APIService APIService; public static APIService getAPIService() { if (APIService == null) { Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()).build(); APIService = retrofit.create(APIService.class); } return APIService; }}
这是我的驱动程序pojo模型(所有pojo模型几乎相同)
public class Driver { @Expose private List<String> driver_name = new ArrayList<String>(); public List<String> getDriver_name() { return driver_name; } public voID setDriver_name(List<String> driver_name) { this.driver_name = driver_name; }}
这是我的片段,我得到了合作伙伴的回调,需要实现另一个按钮来获取驱动程序和第三个按钮来获取一些存储空间.
public class DownloadMain extends Fragment implements Callback<Partner> { private static final String TAG = DownloadMain.class.getSimplename(); private button dloadPartners,takeDrivers,takeWarehouses,takeUsers,takeLogs; private Call callPartners; public DownloadMain() {} public DownloadMain newInstance() { return new DownloadMain(); } @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) { VIEw vIEw = inflater.inflate(R.layout.download_main,container,false); dloadPartners = (button) vIEw.findVIEwByID(R.ID.downloadPartners); takeDrivers = (button) vIEw.findVIEwByID(R.ID.btnDrivers); dloadPartners.setonClickListener(btnListener); takeDrivers.setonClickListener(btnDrivers); callPartners = APIHelper.getAPIService().getPartners(); return vIEw; } button.OnClickListener btnListener = (new button.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { callPartners.clone().enqueue(DownloadMain.this); checkIfDataIsIntable(); } }); @OverrIDe public voID onResponse(Call call,Response response) { if(response.body() == null) { try { response.errorBody().string(); } catch (IOException e) { e.printstacktrace(); } Toast.makeText(getActivity(),"No Partners!",Toast.LENGTH_SHORT).show(); } else { ArrayList<String> partners = (ArrayList<String>) response.body(); ActiveAndroID.beginTransaction(); try { for (int i = 0; i < partners.size() ; i++) { Partners partner = new Partners(); partner.name = String.valueOf(partners); partner.save(); Log.d("partner_ ",String.valueOf(response.body())); } ActiveAndroID.setTransactionSuccessful(); } finally { ActiveAndroID.endTransaction(); } Log.d(TAG,"Number of partners received: " + partners.size()); Toast.makeText(getActivity(),"Partners downloaded!",Toast.LENGTH_SHORT).show(); } } @OverrIDe public voID onFailure(Call call,Throwable t) { }}
现在我无法实现第二个按钮来从API获取数据.
如果有人可以帮我实现第二个按钮来从API获取驱动程序,我将不胜感激?!
解决方法 使用retrofit2,你可以使用call.enqueue内联onResponse和onFailure,它允许你在同一个活动中拥有多个调用类型,因为它们被封装在call.enqueue()中// the following can be in the same activityCall<YourPOJO1> call1 = clIEnt.callname1(params);call1.enqueue(new Callback<YourPOJO1>() { @OverrIDe public voID onResponse(Call<YourPOJO1> call,Response<YourPOJO1> response) { Log.d(TAG,"Call1 Succeeded"); int code = response.code(); if (code == 200) { // your parsing of POJO1 here } else { Log.d(TAG,"Error Happened"); } } @OverrIDe public voID onFailure(Call<YourPOJO1> call,Throwable t) { Log.d(TAG,"Call1 Failed"); } });Call<YourPOJO2> call2 = clIEnt.callname2(params);call2.enqueue(new Callback<YourPOJO2>() { @OverrIDe public voID onResponse(Call<YourPOJO2> call,Response<YourPOJO2> response) { Log.d(TAG,"Call2 Succeeded"); int code = response.code(); if (code == 200) { // your parsing of POJO2 here } else { Log.d(TAG,"Error Happened"); } } @OverrIDe public voID onFailure(Call<YourPOJO2> call,"Call2 Failed"); } });总结
以上是内存溢出为你收集整理的如何在同一个片段中创建多个Retrofit回调 – android全部内容,希望文章能够帮你解决如何在同一个片段中创建多个Retrofit回调 – android所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)