Click here to Skip to main content
13,628,860 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

4.1K views
1 bookmarked
Posted 3 Jul 2015
Licenced CPOL

AndroidServiceClient with Authentication

, 3 Jul 2015
Rate this:
Please Sign up or sign in to vote.
AndroidServiceClient with Authentication

I'm currently researching ServiceStack as a replacement to my own services for handling REST between Android and Windows.

I've got the client (Android) working using loopj's AsyncHttpClient. But I was looking for a more native library. Initially I was using Jsonserviceclient but decided on the AndroidServiceClient. The problem I had was setting up basic authentication on the connection. After a few hours of reading JsonServiceClient.java (which AndroidServiceClient extends), it transpired I have to implement my own request ConnectionFilter and set up the request headers in the void exec(HttpURLConnection urlConnection) method.

After creating code, I got the error "cannot set request property after connection is made". Seems I needed to tweak the code to suppress this error.

CustomRequestFilter...

package com.jjoplc.pod.Views;

import android.util.Base64;
import net.servicestack.client.ConnectionFilter;
import java.net.HttpURLConnection;

/**
 * Created by norm on 01/07/2015.
 */
public class CustomRequestFilter implements ConnectionFilter {

    private String password = "";
    private String username = "";

    static  boolean done = false;
    public void exec(HttpURLConnection urlConnection) {
        if (done) {
            done = false;
            return;
        };

        done = true;

        String credentials = username + ":" + password;
        final String basicAuth = "Basic " + 
        Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
        urlConnection.setRequestProperty("Authorization", basicAuth);
    }

    public void setUsername(String userName) {
        this.username = userName;
    }

    public void setPassword(String passWord) {
        this.password = passWord;
    }
}

Set up.

 _androidClient = new AndroidServiceClient("http://myservice:8088");
_requestFilter = new CustomRequestFilter();
_requestFilter.setUsername("User");
_requestFilter.setPassword("Password");
_androidClient.RequestFilter = _requestFilter;

And test.

androidClient.getAsync(new dto.Hello().setName("Normski"), 
new AsyncResult<dto.HelloResponse>() {
    @Override
    public void success(dto.HelloResponse r) {
        view.setText(r.getResult());
    }

    @Override
    public void error(Exception ex) {
        view.setText(ex.toString());
    }
});

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

NormDroid
Software Developer (Senior) Software Kinetics
United Kingdom United Kingdom




Software Kinetics
are experts in developing customised and bespoke applications and have expertise in the development of desktop, mobile and internet applications on Windows.


We specialise in:

  • User Interface Design
  • Desktop Development
  • Windows Phone Development
  • Windows Presentation Framework
  • Windows Forms
  • Windows Communication Framework
  • Windows Services
  • Network Applications
  • Database Applications
  • Web Development
  • Web Services
  • Silverlight
  • ASP.net


Visit Software Kinetics

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02-2016 | 2.8.180712.1 | Last Updated 3 Jul 2015
Article Copyright 2015 by NormDroid
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid