我正在开发一个连接到API的Android应用。为了发送get请求,我需要发送一个post请求,返回一个验证用户身份的cookie。
如果我尝试在未经过身份验证的情况下发送获取请求,我将收到身份验证失败错误。
所以,我希望我的应用程序能够像这样工作:如果我发送了一个get请求而用户未经过身份验证,请对用户(authenticate
方法)进行身份验证,然后发送get请求。
我尝试在getUserProfile
的开头执行此操作,但由于authenticate
是异步的,因此它将在身份验证完成之前执行get请求。因此,它会导致错误。
if (!AppSingleton.getInstance(context).isUserAuthenticated()) {
authenticate(context, id);
}
public void authenticate (final Context ctx, int ID) {
StringRequest request = new StringRequest(POST, URL + ID , new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
})
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/x-www-form-urlencoded");
Log.d(TAG, "getHeaders: " + headers.toString());
return headers;
}
@Override
protected Response parseNetworkResponse(NetworkResponse response) {
Map headers = response.headers;
AppSingleton.getInstance(ctx).setCookie(headers.get("Set-Cookie").toString());
Log.d(TAG, "parseNetResponse: ");
return super.parseNetworkResponse(response);
}
};
AppSingleton.getInstance(ctx).getRequestQueue().add(request);
}
public void getUserProfile (final Context context, final String userID) {
// Request a string response from the provided URL.
String url = GET_USER_PROFILE_URL + userID;
Log.d(TAG, url);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Not sure if this is a good practice.
ProfileActivity.displayInformation(response);
ProfileActivity.toggleProgress();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, error.toString(), Toast.LENGTH_LONG).show();
}
})
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Cookie", AppSingleton.getInstance(context).getCookie());
headers.put("Content-Type", "application/json");
return headers;
}
};
// Add the request to the RequestQueue.
AppSingleton.getInstance(context).getRequestQueue().add(stringRequest);
}
PS:我希望这个标题有意义。
更新:@muratgu,这是代码。我做错了什么,因为它没有用。
getConversationMessages
:
Response.Listener listener = new Response.Listener() {
@Override
public void onResponse(Object o) {
Log.d(TAG, "getConversations: Facebook Login successful. User ID is " + o.toString());
}
};
Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(context, "getConver - Authentication error: " + volleyError.toString(), Toast.LENGTH_LONG).show();
Log.d(TAG, "getConver - Authentication error: " + volleyError.toString());
}
};
authenticate(context, id, listener, errorListener);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, listener, errorListener) { [...]
authenticate
:
public void authenticate (final Context ctx, int ID, Response.Listener listener, Response.ErrorListener errorListener) {
StringRequest request = new StringRequest(POST, APP_URL + LOGIN_USER_FB_URL + ID , listener, errorListener) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/x-www-form-urlencoded");
return headers;
}
@Override
protected Response parseNetworkResponse(NetworkResponse response) {
Map headers = response.headers;
AppSingleton.getInstance(ctx).setCookie(headers.get("Set-Cookie").toString());
return super.parseNetworkResponse(response);
}
};
AppSingleton.getInstance(ctx).getRequestQueue().add(request);
}
答案 0 :(得分:1)
Response.Listener
和Response.ErrorListener
移至getUserProfile
。authenticate
。authenticate
会在成功或失败时调用侦听器。getUserProfile
将在其Response.Listener
。