[英]Getting a return value from volley onResponse
我正在尝试制作我的第一个真实程序,所以请放轻松。 这部分使用Google API和凌空转换输入的邮政编码为lat / lng坐标,但是我不知道如何获取凌空功能来“返回”坐标。 从我阅读的内容中,我需要实现一些回调方法,但是我不知道该怎么做。
//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
将您的onResponse
方法更改为:
@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 {
useCoordinates(coordinates);
}
}
然后实现使用坐标的方法:
public void useCoordinates(Location coordinates) {
// Use the value
}
我认为您想做这样的事情:
这将是您的回调类:(NetworkResponse.java)
public abstract class NetworkResponse<T> implements Response.Listener<T>, Response.ErrorListener {
@Override
public void onResponse(T t) {
String coordinates = parseLocation(t);
onCoordinatesReceived(coordinates;
}
@Override
public void onErrorResponse(VolleyError volleyError) {
onCoordinatesReceived("");
}
public String parseLocation(T s) {
return null;
//Implement parsing here.
}
public abstract void onCoordinatesReceived(String coordinates) ;
}
并在您现有的班级中进行以下更改:
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
- -至 - -
NetworkResponse<String> networkResponse =
new NetworkResponse<>() {
@Override
public void onCoordinatesReceived(String coordinates){
//Here are your coordinates.
if((coordinates == null && coordinates.isEmpty())||coordinates.equals("NOT_VALID_ADDRESS")) {
showAlert("Please Enter A Valid Zip Code");
else {
//Your implementation here.
}
}
StringRequest address = new StringRequest(Request.Method.GET, addressurl,
networkResponse, networkResponse);
mRequestQueue.add(address); //Add address to ResponseQueue
请注意:此处的回调类中为NetworkResponse。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.