在返回函数之前等待 JSON 响应?
Waiting for JSON response before returning the function?
大家好,我正在使用 Android Volley 库创建一个 android 应用程序的 login/register 部分。我的应用程序运行良好,但 UI 和逻辑相同 class。所以,我把它们分成了两个classes。我的应用程序使用 POST 方法向我的 NodeJS 服务器发出请求并获得 JSON 响应。所以我试图将 POST 请求函数保留在另一个 class 中。
分离 classes 后,我在等待响应时遇到问题。这是函数;
public String doWebRequestLogin(Context context, boolean checkLoginForm, final Map<String,String> json){
result[0] = "FREE";
this.context = context;
if(checkLoginForm){
StringRequest post = new StringRequest(Request.Method.POST, loginUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
Log.d("Login Response: ",response);
data = response;
res = new JSONObject(data);
if (res.getString(KEY_SUCCESS) != null) {
int success = Integer.parseInt(res.getString(KEY_SUCCESS));
if (success == 1) {
result[0] = "LOGGED";
} else if (success == 0) {
result[0] = "LOGIN ERROR";
} else {
result[0] = "INVALID POST";
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Response Error", error.toString());
result[0] = "INVALID POST";
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> map = json;
return map;
}
};
VolleyController.getInstance(this.context).getRequestQueue().add(post);
}
return result[0];
}
这个函数 returns 结果[0] 由于响应时间的原因每次都是 "FREE"。它如何等待响应并根据响应设置result[0]?我需要知道在提出请求时发生了什么。
请求是异步的,您不能阻塞主线程等待响应。使方法无效并在收到响应后使用回调处理响应。
public void doWebRequestLogin(SomeCallback callback, Context context, boolean checkLoginForm, final Map<String,String> json){
[...]
if (res.getString(KEY_SUCCESS) != null) {
int success = Integer.parseInt(res.getString(KEY_SUCCESS));
callback.someMethod(success);
}
}
回调:
public interface SomeCallback{
void someMethod(int result); // response received, handle it
}
回调也可能有 return 类型或者是通用的,这完全取决于您的需要...
I'm calling doWebRequestLogin() on the UI within an onclick function
那么你不想要"wait for the response"。无论网络 I/O 占用多长时间,这都会冻结您的 UI,并且您的用户将……不为所动。
相反,请在 onResponse()
和 onErrorResponse()
方法中更新您的 UI。
这种通过回调处理结果的异步调用是 Android 核心的事件驱动编程模型的核心。
大家好,我正在使用 Android Volley 库创建一个 android 应用程序的 login/register 部分。我的应用程序运行良好,但 UI 和逻辑相同 class。所以,我把它们分成了两个classes。我的应用程序使用 POST 方法向我的 NodeJS 服务器发出请求并获得 JSON 响应。所以我试图将 POST 请求函数保留在另一个 class 中。
分离 classes 后,我在等待响应时遇到问题。这是函数;
public String doWebRequestLogin(Context context, boolean checkLoginForm, final Map<String,String> json){
result[0] = "FREE";
this.context = context;
if(checkLoginForm){
StringRequest post = new StringRequest(Request.Method.POST, loginUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
Log.d("Login Response: ",response);
data = response;
res = new JSONObject(data);
if (res.getString(KEY_SUCCESS) != null) {
int success = Integer.parseInt(res.getString(KEY_SUCCESS));
if (success == 1) {
result[0] = "LOGGED";
} else if (success == 0) {
result[0] = "LOGIN ERROR";
} else {
result[0] = "INVALID POST";
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Response Error", error.toString());
result[0] = "INVALID POST";
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> map = json;
return map;
}
};
VolleyController.getInstance(this.context).getRequestQueue().add(post);
}
return result[0];
}
这个函数 returns 结果[0] 由于响应时间的原因每次都是 "FREE"。它如何等待响应并根据响应设置result[0]?我需要知道在提出请求时发生了什么。
请求是异步的,您不能阻塞主线程等待响应。使方法无效并在收到响应后使用回调处理响应。
public void doWebRequestLogin(SomeCallback callback, Context context, boolean checkLoginForm, final Map<String,String> json){
[...]
if (res.getString(KEY_SUCCESS) != null) {
int success = Integer.parseInt(res.getString(KEY_SUCCESS));
callback.someMethod(success);
}
}
回调:
public interface SomeCallback{
void someMethod(int result); // response received, handle it
}
回调也可能有 return 类型或者是通用的,这完全取决于您的需要...
I'm calling doWebRequestLogin() on the UI within an onclick function
那么你不想要"wait for the response"。无论网络 I/O 占用多长时间,这都会冻结您的 UI,并且您的用户将……不为所动。
相反,请在 onResponse()
和 onErrorResponse()
方法中更新您的 UI。
这种通过回调处理结果的异步调用是 Android 核心的事件驱动编程模型的核心。