2024.4.23
第三十五天
所花时间:2小时
代码量:400+
博客量:1
了解到的知识点:Android端代码
api包
package com.example.metro_info_front_end.api; import com.example.metro_info_front_end.DataModel.MetroLine; import com.example.metro_info_front_end.DataModel.MetroStation; import com.example.metro_info_front_end.DataModel.MetroSystem; import java.util.List; import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.Path; import retrofit2.http.Query; public interface ApiService { // 获取所有地铁系统信息 @GET("/metro/find/system") Call<List<MetroSystem>> getAllMetroSystems(); // 根据城市和线路名称查询线路上所有站点 @GET("/metro/{systemName}/line/{lineNumber}/stations") Call<List<MetroStation>> getStationsBySystemAndLine( @Path("systemName") String systemName, @Path("lineNumber") String lineNumber ); // 根据系统 ID 和站点名称模糊查询站点 @GET("/metro/{sid}/find/station") Call<List<MetroStation>> findMetroStation( @Path("sid") String sid, @Query("stName") String stName ); // 根据系统 ID 和线路编号查询线路信息 @GET("/metro/{sid}/search/line") Call<List<MetroSystem>> searchByLine( @Path("sid") String sid, @Query("lNum") String lNum ); // 根据系统 ID、起始站点和目标站点查询最优路线 @GET("/metro/{sid}/search/route") Call<List<MetroStation>> searchShortestPath( @Path("sid") String sid, @Query("from") String from, @Query("to") String to ); // 根据地铁系统名称查询线路信息 @GET("/metro/find/lines/{systemName}") Call<List<MetroLine>> getLinesBySystemName( @Path("systemName") String systemName ); // 根据系统名称查询对应的系统编码 @GET("/metro/findSystemCode") Call<String> findSystemCodeBySystemName(@Query("systemName") String systemName); }
package com.example.metro_info_front_end.api; import okhttp3.OkHttpClient; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; import okhttp3.logging.HttpLoggingInterceptor; public class RetrofitClient { private static Retrofit retrofit; private static final String BASE_URL = "http://192.168.43.157:8080/"; public static Retrofit getRetrofitInstance() { if (retrofit == null) { // 创建 OkHttpClient 并添加日志拦截器 OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); httpClient.addInterceptor(loggingInterceptor); retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .client(httpClient.build()) // 使用带有日志拦截器的 OkHttpClient .build(); } return retrofit; } }