
如何获取ANTHROPIC 开放平台 API Key 密钥(分步指南)
在开始开发之前,请确保计算机上已安装以下工具:
此外,您需要 注册 Skyscanner API 并获取 API Key,以便进行开发和测试。
TravelPlanner
。MainActivity
和 activity_main.xml
。在项目的 build.gradle
文件中添加 Retrofit 依赖:
implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "com.squareup.retrofit2:converter-gson:2.9.0"
同步项目以下载库文件。
a. 在 app/src/main/java/your/package/name
下创建新包 api
。
b. 新建接口 SkyscannerService.java
:
public interface SkyscannerService {
@GET("browsolutions/v1.0/{market}/{currency}/{locale}/{originPlace}/{destinationPlace}/{outboundPartialDate}/{inboundPartialDate}")
Call<FlightResponse> getFlights(
@Path("market") String market,
@Path("currency") String currency,
@Path("locale") String locale,
@Path("originPlace") String originPlace,
@Path("destinationPlace") String destinationPlace,
@Path("outboundPartialDate") String outboundPartialDate,
@Path("inboundPartialDate") String inboundPartialDate,
@Query("apiKey") String apiKey
);
}
说明:
FlightResponse
为模型类,用于解析 Skyscanner API 返回的 JSON 数据。请根据实际 API 响应结构定义对应字段。
在 res/layout/activity_main.xml
中添加以下 UI 元素:
示例布局:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<EditText
android:id="@+id/etOrigin"
android:hint="出发地"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/etDestination"
android:hint="目的地"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnSearch"
android:text="搜索航班"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvFlights"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
创建 FlightAdapter.java
,用于将航班数据绑定到 RecyclerView。
RecyclerView recyclerView = findViewById(R.id.rvFlights);
FlightAdapter adapter = new FlightAdapter(new ArrayList<>());
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
Button btnSearch = findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(v -> {
String origin = ((EditText)findViewById(R.id.etOrigin)).getText().toString();
String destination = ((EditText)findViewById(R.id.etDestination)).getText().toString();
SkyscannerService service = RetrofitClient.getInstance().create(SkyscannerService.class);
Call<FlightResponse> call = service.getFlights(
"US", "USD", "en-US",
origin, destination,
"2025-09-15", "2025-09-20",
"YOUR_API_KEY"
);
call.enqueue(new Callback<FlightResponse>() {
@Override
public void onResponse(Call<FlightResponse> call, Response<FlightResponse> response) {
if(response.isSuccessful() && response.body() != null){
adapter.updateData(response.body().getFlights());
}
}
@Override
public void onFailure(Call<FlightResponse> call, Throwable t) {
Toast.makeText(TravelPlanner.this, "获取航班失败", Toast.LENGTH_SHORT).show();
}
});
});
说明:
RetrofitClient
为单例 Retrofit 实例类,确保网络请求统一管理。
如遇问题,可通过 Logcat 调试 API 响应及数据绑定逻辑。
通过本教程,您已完成:
后续可扩展功能:
借助 Skyscanner API,您的应用将成为一个高效的旅行规划工具,为用户提供实时、精准的航班信息。
原文链接: https://reintech.io/blog/creating-travel-planning-app-android-skyscanner-api