2015-06-17 176 views
0

我正在開發連接到API的Android應用程序。爲了發送獲取請求,我需要發送一個發佈請求,返回一個驗證用戶身份的cookie。如果用戶在提出請求之前未通過身份驗證,則對用戶進行身份驗證

如果我嘗試發送沒有認證的獲取請求,我會得到一個認證失敗錯誤。因此,我希望我的應用程序能夠像這樣工作:如果我發送獲取請求並且未驗證用戶身份,請驗證用戶身份(authenticate方法),然後發送獲取請求。

我試圖在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); 
} 

回答

1
  1. 移動Response.ListenerResponse.ErrorListenergetUserProfile
  2. 將聽衆傳遞給authenticate作爲參數。
  3. authenticate會在成功或失敗時調用監聽器。
  4. getUserProfile將在其Response.Listener內完成其工作。
+0

我試過了,但它不工作。 –

相關問題