gpt4 book ai didi

android - 如何在同一个 fragment 中创建多个 Retrofit 回调 - android

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:56:37 24 4
gpt4 key购买 nike

我试图在 fragment 中实现多个按钮,每个按钮都需要从网络 API 获取一些数据。

我已经为一个按钮创建了改造回调。现在我在创建另一个按钮以从另一个 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; }
}

这是我的 fragment ,我在其中获得了合作伙伴的回调,需要实现另一个按钮来获取驱动程序和第三个按钮来获取一些存储空间。

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,您可以将 onResponse 和 onFailure 与 call.enqueue 内联,这让您可以在同一 Activity 中拥有多个调用类型,因为它们被封装在 call.enqueue() 中

// the following can be in the same activity
Call<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, Throwable t) {
Log.d(TAG, "Call2 Failed");
}
});

关于android - 如何在同一个 fragment 中创建多个 Retrofit 回调 - android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39378586/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com