@GET 中的两个查询参数
Two query paramethers in @GET
我正在做一个项目,我必须使用 retrofit2 从 API 获取数据。
我的url是这样的:https://nominatim.openstreetmap.org/search
在 "search" 之后,我有一个键 (q),其中引入了城市名称和一个特殊的词(查询),空格被替换为“+”信号。
键的末尾必须有“&format=json”
这是 Postman 上的示例:
只有一件事:usa+fuel 这只是一个示范性的例子。我不仅需要在美国搜索,还需要在全世界搜索。在 resumen 中,可以使用以下端点返回的本地列表中的 local_id 查询位置的详细信息:
https://nominatim.openstreetmap.org/details?place_id=194336640&format=json
用它的 id
替换 place_id 参数
该应用程序还应有助于装载站的地理空间搜索,即,在地图上提供基于纬度和经度的每个站的位置。为目的
nominatim API 提供将 GPS 坐标转换为地址的服务
(https://nominatim.org/release-docs/develop/api/Lookup/)
我如何在 android 中执行此操作?
您可以在改造中使用 DYNAMIC URL 和 @Get。
你的界面是这样的:
@Headers("Content-Type: application/json")
@GET
Call<ModelName> getResponseUrl(@Url String url);
完整示例代码:
ApiInterface:
public interface ApiInterface {
@Headers("Content-Type: application/json")
@GET
Call<List<StreetMap>> getResponseUrl(@Url String url);
}
型号-StreetMap.java:
public class StreetMap {
@SerializedName("place_id")
@Expose
private Long placeId;
@SerializedName("licence")
@Expose
private String licence;
@SerializedName("osm_type")
@Expose
private String osmType;
@SerializedName("osm_id")
@Expose
private Long osmId;
@SerializedName("boundingbox")
@Expose
private List<String> boundingbox = null;
@SerializedName("lat")
@Expose
private String lat;
@SerializedName("lon")
@Expose
private String lon;
@SerializedName("display_name")
@Expose
private String displayName;
@SerializedName("class")
@Expose
private String _class;
@SerializedName("type")
@Expose
private String type;
@SerializedName("importance")
@Expose
private Double importance;
@SerializedName("icon")
@Expose
private String icon;
public Long getPlaceId() {
return placeId;
}
public void setPlaceId(Long placeId) {
this.placeId = placeId;
}
public String get_class() {
return _class;
}
public void set_class(String _class) {
this._class = _class;
}
public String getLicence() {
return licence;
}
public void setLicence(String licence) {
this.licence = licence;
}
public String getOsmType() {
return osmType;
}
public void setOsmType(String osmType) {
this.osmType = osmType;
}
public Long getOsmId() {
return osmId;
}
public void setOsmId(Long osmId) {
this.osmId = osmId;
}
public List<String> getBoundingbox() {
return boundingbox;
}
public void setBoundingbox(List<String> boundingbox) {
this.boundingbox = boundingbox;
}
public String getLat() {
return lat;
}
public void setLat(String lat) {
this.lat = lat;
}
public String getLon() {
return lon;
}
public void setLon(String lon) {
this.lon = lon;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getClass_() {
return _class;
}
public void setClass_(String _class) {
this._class = _class;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Double getImportance() {
return importance;
}
public void setImportance(Double importance) {
this.importance = importance;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
}
APIClient.java:
public class APIClient {
private static Retrofit retrofit = null;
static Retrofit getClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
retrofit = new Retrofit.Builder()
.baseUrl("https://nominatim.openstreetmap.org/")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit;
}
}
Activity代码:
public class MainActivity extends AppCompatActivity {
ApiInterface apiInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
apiInterface = APIClient.getClient().create(ApiInterface.class);
String cityName="usa+fuel";
Call<List<StreetMap>> callUrl = apiInterface.getResponseUrl("/search?q="+cityName+"&format=json");
callUrl.enqueue(new Callback<List<StreetMap>>() {
@Override
public void onResponse(Call<List<StreetMap>> call, Response<List<StreetMap>> response) {
Toast.makeText(getApplicationContext(),"Success Response",Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<List<StreetMap>> call, Throwable t) {
Toast.makeText(getApplicationContext(),"Error in Response:"+t.getMessage(),Toast.LENGTH_SHORT).show();
}
});
}
}
使用 headers 改造后的网络调用界面将如下所示:
@Headers({
"Content-Type: application/json",
"User-Agent: Your-App-Name",
"Cache-Control: max-age=640000"
})
@GET("search")
Call<String> getData(@Query("q") String city_name,@Query("format") String format)
我正在做一个项目,我必须使用 retrofit2 从 API 获取数据。
我的url是这样的:https://nominatim.openstreetmap.org/search
在 "search" 之后,我有一个键 (q),其中引入了城市名称和一个特殊的词(查询),空格被替换为“+”信号。 键的末尾必须有“&format=json” 这是 Postman 上的示例:
只有一件事:usa+fuel 这只是一个示范性的例子。我不仅需要在美国搜索,还需要在全世界搜索。在 resumen 中,可以使用以下端点返回的本地列表中的 local_id 查询位置的详细信息: https://nominatim.openstreetmap.org/details?place_id=194336640&format=json 用它的 id
替换 place_id 参数该应用程序还应有助于装载站的地理空间搜索,即,在地图上提供基于纬度和经度的每个站的位置。为目的 nominatim API 提供将 GPS 坐标转换为地址的服务 (https://nominatim.org/release-docs/develop/api/Lookup/)
我如何在 android 中执行此操作?
您可以在改造中使用 DYNAMIC URL 和 @Get。
你的界面是这样的:
@Headers("Content-Type: application/json")
@GET
Call<ModelName> getResponseUrl(@Url String url);
完整示例代码:
ApiInterface:
public interface ApiInterface {
@Headers("Content-Type: application/json")
@GET
Call<List<StreetMap>> getResponseUrl(@Url String url);
}
型号-StreetMap.java:
public class StreetMap {
@SerializedName("place_id")
@Expose
private Long placeId;
@SerializedName("licence")
@Expose
private String licence;
@SerializedName("osm_type")
@Expose
private String osmType;
@SerializedName("osm_id")
@Expose
private Long osmId;
@SerializedName("boundingbox")
@Expose
private List<String> boundingbox = null;
@SerializedName("lat")
@Expose
private String lat;
@SerializedName("lon")
@Expose
private String lon;
@SerializedName("display_name")
@Expose
private String displayName;
@SerializedName("class")
@Expose
private String _class;
@SerializedName("type")
@Expose
private String type;
@SerializedName("importance")
@Expose
private Double importance;
@SerializedName("icon")
@Expose
private String icon;
public Long getPlaceId() {
return placeId;
}
public void setPlaceId(Long placeId) {
this.placeId = placeId;
}
public String get_class() {
return _class;
}
public void set_class(String _class) {
this._class = _class;
}
public String getLicence() {
return licence;
}
public void setLicence(String licence) {
this.licence = licence;
}
public String getOsmType() {
return osmType;
}
public void setOsmType(String osmType) {
this.osmType = osmType;
}
public Long getOsmId() {
return osmId;
}
public void setOsmId(Long osmId) {
this.osmId = osmId;
}
public List<String> getBoundingbox() {
return boundingbox;
}
public void setBoundingbox(List<String> boundingbox) {
this.boundingbox = boundingbox;
}
public String getLat() {
return lat;
}
public void setLat(String lat) {
this.lat = lat;
}
public String getLon() {
return lon;
}
public void setLon(String lon) {
this.lon = lon;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getClass_() {
return _class;
}
public void setClass_(String _class) {
this._class = _class;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Double getImportance() {
return importance;
}
public void setImportance(Double importance) {
this.importance = importance;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
}
APIClient.java:
public class APIClient {
private static Retrofit retrofit = null;
static Retrofit getClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
retrofit = new Retrofit.Builder()
.baseUrl("https://nominatim.openstreetmap.org/")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit;
}
}
Activity代码:
public class MainActivity extends AppCompatActivity {
ApiInterface apiInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
apiInterface = APIClient.getClient().create(ApiInterface.class);
String cityName="usa+fuel";
Call<List<StreetMap>> callUrl = apiInterface.getResponseUrl("/search?q="+cityName+"&format=json");
callUrl.enqueue(new Callback<List<StreetMap>>() {
@Override
public void onResponse(Call<List<StreetMap>> call, Response<List<StreetMap>> response) {
Toast.makeText(getApplicationContext(),"Success Response",Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<List<StreetMap>> call, Throwable t) {
Toast.makeText(getApplicationContext(),"Error in Response:"+t.getMessage(),Toast.LENGTH_SHORT).show();
}
});
}
}
使用 headers 改造后的网络调用界面将如下所示:
@Headers({
"Content-Type: application/json",
"User-Agent: Your-App-Name",
"Cache-Control: max-age=640000"
})
@GET("search")
Call<String> getData(@Query("q") String city_name,@Query("format") String format)