【发布时间】:2016-06-15 15:15:08
【问题描述】:
我正在尝试制作我的第一个真正的程序,所以请放轻松。 这部分使用 Google API 和 volley 将输入的邮政编码转换为 lat/lng 坐标,但我不知道如何让 volley 函数“返回”坐标。根据我的阅读,我需要实现一些回调方法,但我不知道该怎么做。
//Converts values in TextView to String
locationurl = address.getText().toString();
//RequestQueue Variable
RequestQueue mRequestQueue;
// Instantiate the cache and set up the network to use HttpURLConnection as the HTTP client.
Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap
BasicNetwork network = new BasicNetwork(new HurlStack());
//Instantiate the RequestQueue with the cache and network
mRequestQueue = new RequestQueue(cache, network);
//Start Queue
mRequestQueue.start();
String addressurl = "http://maps.googleapis.com/maps/api/geocode/json?address=" + location;
//Formulate the request and handle the response
StringRequest address = new StringRequest(Request.Method.GET, addressurl,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
coordinates = parseLocation(response);
if((coordinates == null && coordinates.isEmpty())||coordinates.equals("NOT_VALID_ADDRESS")) {
showAlert("Please Enter A Valid Zip Code");
} else {
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
showAlert("Error."); //Error code display
//TODO: fix error handling
}
});
mRequestQueue.add(address); //Add address to ResponseQueue
【问题讨论】:
-
new Response.Listener<String>()是回调
标签: java android json android-volley