我在MainActivity中有一个搜索对话框,在使用时会将用户查询发送到SearchActivity。从那里我使用volley发送JSON请求,然后使用GSON反序列化JSON。所有这一切都很好,我甚至可以使用以下方式打印我的请求的纬度值:
System.out.println(cam[0].getLat());
但是,所有这一切都必须在截击请求的onResponse中完成。我试图将一些值发送回我的MainActivity,我知道该怎么做。我只需要知道如何存储一些字符串,以便我可以在onResponse之外访问它们。
发送请求并反序列化的方法:
private void fetchJsonResponse() {
JsonArrayRequest req = new JsonArrayRequest(myUrl, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Gson gson = new Gson();
String nominatimData = response.toString();
NominatimModel[] cam = gson.fromJson(nominatimData, NominatimModel[].class);
// i'd like to using cam[0].getLat(); outside of this method
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
mRequestQueue.add(req);
}
NominatimModel.java
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.List;
public class NominatimModel {
@SerializedName("place_id")
@Expose
private String placeId;
@Expose
private String licence;
@SerializedName("osm_type")
@Expose
private String osmType;
@SerializedName("osm_id")
@Expose
private String osmId;
@Expose
private List<String> boundingbox = new ArrayList<String>();
@Expose
private String lat;
@Expose
private String lon;
@SerializedName("display_name")
@Expose
private String displayName;
@SerializedName("class")
@Expose
private String _class;
@Expose
private String type;
@Expose
private Double importance;
@Expose
private String icon;
/**
* @return The placeId
*/
public String getPlaceId() {
return placeId;
}
/**
* @param placeId The place_id
*/
public void setPlaceId(String placeId) {
this.placeId = placeId;
}
/**
* @return The licence
*/
public String getLicence() {
return licence;
}
/**
* @param licence The licence
*/
public void setLicence(String licence) {
this.licence = licence;
}
/**
* @return The osmType
*/
public String getOsmType() {
return osmType;
}
/**
* @param osmType The osm_type
*/
public void setOsmType(String osmType) {
this.osmType = osmType;
}
/**
* @return The osmId
*/
public String getOsmId() {
return osmId;
}
/**
* @param osmId The osm_id
*/
public void setOsmId(String osmId) {
this.osmId = osmId;
}
/**
* @return The boundingbox
*/
public List<String> getBoundingbox() {
return boundingbox;
}
/**
* @param boundingbox The boundingbox
*/
public void setBoundingbox(List<String> boundingbox) {
this.boundingbox = boundingbox;
}
/**
* @return The lat
*/
public String getLat() {
return lat;
}
/**
* @param lat The lat
*/
public void setLat(String lat) {
this.lat = lat;
}
/**
* @return The lon
*/
public String getLon() {
return lon;
}
/**
* @param lon The lon
*/
public void setLon(String lon) {
this.lon = lon;
}
/**
* @return The displayName
*/
public String getDisplayName() {
return displayName;
}
/**
* @param displayName The display_name
*/
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
/**
* @return The _class
*/
public String getClass_() {
return _class;
}
/**
* @param _class The class
*/
public void setClass_(String _class) {
this._class = _class;
}
/**
* @return The type
*/
public String getType() {
return type;
}
/**
* @param type The type
*/
public void setType(String type) {
this.type = type;
}
/**
* @return The importance
*/
public Double getImportance() {
return importance;
}
/**
* @param importance The importance
*/
public void setImportance(Double importance) {
this.importance = importance;
}
/**
* @return The icon
*/
public String getIcon() {
return icon;
}
/**
* @param icon The icon
*/
public void setIcon(String icon) {
this.icon = icon;
}
}
示例JSON:
[{"place_id":"115063146","licence":"Data © OpenStreetMap contributors, ODbL 1.0. http:\/\/www.openstreetmap.org\/copyright","osm_type":"way","osm_id":"258132245","boundingbox":["29.7156836","29.7278762","-95.3520172","-95.3349753"],"lat":"29.7214917","lon":"-95.3440202128152","display_name":"University of Houston, 4800, Calhoun Road, Houston, Harris County, Texas, 77004, United States of America","class":"amenity","type":"university","importance":0.84733963787614,"icon":"http:\/\/nominatim.openstreetmap.org\/images\/mapicons\/education_university.p.20.png"}]
我对java和Android开发相当新,所以如果有人可以帮助我,我会很感激,如果我遗漏任何信息,请告诉我。
答案 0 :(得分:0)
现在我已回答这个问题,时间过去了,我已经学到了很多关于Android的知识,包括如何实现这一目标。所以我让我的所有模型都实现了Serializable,然后将响应数据捆绑在一起并将其传递给其他活动。我后来,摆脱了这个,并且能够在onResponse中完成我所需要的一切。
如果您必须完成我想要完成的任务,我会避免使用Serializable,而是检查Parcelable界面,它具有更快的优势。
希望这有助于遇到此问题的任何人!