4597

Interceptor in Android

Question:

I have an Android app, which require to check authentication token every time perform action to call a web service, therefore I had to included the checking code snippet in all activities. I would like to ask whether an interceptor exist in Android platform, if yes, please share the best practice to apply the interceptor to my code? Thanks.

Answer1:

Using run time reflection is not recommended for android, so you should not use any library which provides run time intercepting your HTTP request. You can write a simple HTTP request utility method and route all your HTTP requests through this method.

public static void makePostRequest(String url, Object body, Context context, AsyncHttpResponseHandler handler) { // here you can add your authentication token to header}

Answer2:

I saw that you want to avoid multiple authentication checking. So, think about creating a custom class which will check for authentication token, then in all your activities, extend that class. For example:

public abstract class MyActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Your authentication code goes here } } public class MainActivity extends MyActivity { // Your concrete class }

This way, you can forget about putting authentication code everywhere.

Recommend

  • MVC - Dynamically loading Partial Views
  • How to develop MS CRM kind of application
  • Should a C# struct have only read-only properties
  • Whats the best way of persisting data to Isolated Storage on Windows Phone 7?
  • Allocating a 2D contiguous array within a function
  • What version of Java should I use with Cassandra 2.0?
  • Consuming a web service with the Netbeans Platform
  • Refactoring advice: maps to POJOs
  • Is it better to use the “hidden” CSS attribute or fetch each set of new images?
  • Translating C# to PowerShell in InterIMAP
  • How to use Windows Media Foundation with UWP without a topology
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • Detecting null parameter in preprocessor macro
  • Is there a way to save the selected text and highlight it again once the page is refreshed?
  • Updating both a ConcurrentHashMap and an AtomicInteger safely
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • Not able to aggregate on nested fields in elasticsearch
  • Reduction and collapse clauses in OMP have some confusing points
  • Unable to install Git-core+svn by MacPorts
  • Django simple Captcha “No module named fields” error
  • Assign variable to the value in HTML
  • x64 applications using gdi+: what are the consequences on performance?
  • Xcode 4 NSLog Macro link in Xcode 3
  • MongoDb aggregation
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • Could not find rake using whenever rails
  • Disable Enter in editText android
  • Cannot resolve symbol 'MyApi'
  • If I include Java 8 in my Android app does that affect which devices it will work on?
  • How would I use PHP exceptions to define a redirect?
  • Release, debug version and Authorization Google?
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • SQL merge duplicate rows and join values that are different
  • WPF Applying a trigger on binding failure
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • File not found error Google Drive API
  • Qt: Run a script BEFORE make