我有一个api链接我希望将参数传递给使用get retrofit方法。链接是
/ambulance/location?latitude=-1.6078289&longitude=38.779225&limit=10
我有下面的Retrofit 2课程:
@GET("/ambulance/location")
Call<AmbulanceModel> getNearbyAmbulances(@Query("latitude") String latitude,
@Query("longitude") String longitude,
@Query("limit") int limit);
在我的主要活动中,我有以下代码;
Call<AmbulanceModel> call = api.getNearbyAmbulances(latitude + " ", longitude + " " , LIMIT);
call.enqueue(new Callback<AmbulanceModel>() {
@Override
public void onResponse(Call<AmbulanceModel> call, Response<AmbulanceModel> response) {
if (response.isSuccessful()) {
ambulanceList = (ArrayList<AmbulanceList>) response.body().getData();
for (int i = 0; i < ambulanceList.size(); i++) {
AmbulanceList ambulance = ambulanceList.get(i);
mAmbulanceAdapter.addAmbulance(ambulance);
}
}
问题是,我得到一个空白/空白页面(没有结果)。我认为Retrofit不会将参数发送到我的服务器api。我已经检查了logcat的错误,但没有任何帮助。
这是在retrofit get方法中将参数传递给api的正确方法吗?
模型类对象模型
public class AmbulanceModel implements Serializable {
private Integer success;
private String message;
private Object error;
private List<AmbulanceList> data = new ArrayList<AmbulanceList>(); //array
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
/**
*
* @return
* The success
*/
public Integer getSuccess() {
return success;
}
/**
*
* @param success
* The success
*/
public void setSuccess(Integer success) {
this.success = success;
}
/**
*
* @return
* The message
*/
public String getMessage() {
return message;
}
/**
*
* @param message
* The message
*/
public void setMessage(String message) {
this.message = message;
}
/**
*
* @return
* The error
*/
public Object getError() {
return error;
}
/**
*
* @param error
* The error
*/
public void setError(Object error) {
this.error = error;
}
/**
*
* @return
* The data
*/
public List<AmbulanceList> getData() {
return data;
}
/**
*
* @param data
* The data
*/
public void setData(List<AmbulanceList> data) {
this.data = data;
}
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
阵列模型
public class AmbulanceList implements Serializable {
private Integer id;
private String name;
private String locationName;
private String latitude;
private String longitude;
private String dateAdded;
private String distance;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
/**
*
* @return
* The id
*/
public Integer getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(Integer id) {
this.id = id;
}
/**
*
* @return
* The name
*/
public String getName() {
return name;
}
/**
*
* @param name
* The name
*/
public void setName(String name) {
this.name = name;
}
/**
*
* @return
* The locationName
*/
public String getLocationName() {
return locationName;
}
/**
*
* @param locationName
* The location_name
*/
public void setLocationName(String locationName) {
this.locationName = locationName;
}
/**
*
* @return
* The latitude
*/
public String getLatitude() {
return latitude;
}
/**
*
* @param latitude
* The latitude
*/
public void setLatitude(String latitude) {
this.latitude = latitude;
}
/**
*
* @return
* The longitude
*/
public String getLongitude() {
return longitude;
}
/**
*
* @param longitude
* The longitude
*/
public void setLongitude(String longitude) {
this.longitude = longitude;
}
/**
*
* @return
* The dateAdded
*/
public String getDateAdded() {
return dateAdded;
}
/**
*
* @param dateAdded
* The date_added
*/
public void setDateAdded(String dateAdded) {
this.dateAdded = dateAdded;
}
/**
*
* @return
* The distance
*/
public String getDistance() {
return distance;
}
/**
*
* @param distance
* The distance
*/
public void setDistance(String distance) {
this.distance = distance;
}
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
json
{
"success": 1,
"message": "Success",
"error": null,
"data": [
{
"id": 1,
"name": "St John Ambulance Kenya",
"location_name": "Nairobi",
"latitude": "-1.2913421",
"longitude": "36.8208557",
"date_added": "2016-09-26T00:45:38Z",
"distance": 10.378971445601792
},
{
"id": 2,
"name": "AMREF Flying Doctors",
"location_name": "Wilson Airport",
"latitude": "-1.318542",
"longitude": "36.7445514",
"date_added": "2016-09-26T00:49:11Z",
"distance": 12.884862762969318
},
答案 0 :(得分:0)
检查您的基本URL(在创建Retrofit实例时使用)是否以“/”结尾。同时删除@GET注释中的第一个“/”:
@GET("ambulance/location")