Jor*_*geR 20 java android wait android-volley
我需要执行一个Volley请求并等待响应解析它并返回它,但不知道如何实现这一点.有人可以帮忙吗?
我现在拥有的是:
public String getLink() {
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
shortenURL = response.getString("url");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
queue.add(jsObjRequest);
return shortenURL;
}
Run Code Online (Sandbox Code Playgroud)
Div*_*mmy 25
Factory方法设计模式解决方案:
要从其他方法返回或获取Volley响应,您需要编写一个Callback功能,使用Interfaces很容易
这个简单的解决方案取自我的MVC架构,用于我开发的Android应用程序,具有完全的可用性和关注点分离概念.
假设JSONObject是来自服务器的响应
步骤1)
创建一个接口 ServerCallback
package xx.xx.xx.utils;
import org.json.JSONObject;
public interface ServerCallback{
void onSuccess(JSONObject result);
}
Run Code Online (Sandbox Code Playgroud)
步骤2)假设您的Volley服务器请求方法在Controller类中,在您的任何Activity中执行此操作
Controller controller = new Controller();
controller.youFunctionForVolleyRequest(email, password, this, loginUrl, new ServerCallback() {
@Override
public void onSuccess(JSONObject response) {
// do stuff here
}
}
);
Run Code Online (Sandbox Code Playgroud)
3)在你的Controller类中,调用inResponse()中的ServerCallback函数,它将 在Activity中执行你的代码,只有在完成服务器任务时才会响应!
public void youFunctionForVolleyRequest(final String email , final String password ,final Context context , final String URL, final ServerCallback callback)
{
HashMap<String, String> params = new HashMap<String, String>();
params.put("email", email);
params.put("password", password);
Log.e("sending json",params.toString());
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
URL, new JSONObject(params), new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
callback.onSuccess(response); // call call back function here
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//VolleyLog.d("Volley error json object ", "Error: " + error.getMessage());
}
}){
@Override
public String getBodyContentType()
{
return "application/json";
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
Run Code Online (Sandbox Code Playgroud)
Que*_*ein 12
您可能不应该返回方法中的链接.
Volley正在执行异步任务,这意味着您无法知道答案何时从您的Web服务到达,可能是10秒或10分钟.
如果你需要函数中的字符串,你应该创建一个方法,并在得到结果时调用它.
这是一个例子:
我想这就是你所拥有的
public void getTheLinkAndCallDoSomething(){
String link = getLink();
doSomethingWithTheLink(link);
}
Run Code Online (Sandbox Code Playgroud)
如果getLink()以同步方式回答,这将起作用.排球不是这种情况.
这就是你可以用Volley做的事情:
public void getTheLinkAndCallDoSomething(){
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET,
url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
shortenURL = response.getString("url");
doSomethingWithTheLink(shortenURL);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
queue.add(jsObjRequest);
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您调用WS来获取您的URL.一旦知道结果,你就打电话doSomethingWithTheLink()
无论如何,如果你真的想要同步,你可以看看这篇文章:等待Async Volley请求的结果并将其返回
此外,请注意等待答案可能会冻结您的应用UI,我想这不是您想要的.
希望它有所帮助.
| 归档时间: |
|
| 查看次数: |
43847 次 |
| 最近记录: |