REST API with HTTP Authentication – Android & Beanstalk example

REST API with HTTP Authentication – Android & Beanstalk example

Hello everyone. For the last few months I have had a lot of work on my hands, thus ignoring my Android development skills. Lately however, seems like my appetite for mobile apps development is back. Or is it just my way of escaping the everyday struggle with large Magento database and sea of custom code. Anyhow, Android platform is a great exhaust valve for me.

There has been some talk about our company starting to use Beanstalk service (boss if you are reading this, I hope you get us Beanstalk account :)). Anyhow, one of the cool things I like about the Beanstalk is their REST API, more info here

This was more then enough for me to start poking with Android, HTTP Authentication, consuming REST service, etc.

Here is an example of REST API method call from Android that does the username/password authentication on the Beanstalk service. You can check this url for the detailed info on what the response returns.

package net.ajzele.beanstalk.stalkmanager;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;
import android.os.Bundle;
import android.util.Log;
public class HomeActivity extends Activity {
	String MY_APP_TAG = "net.ajzele.beanstalk.stalkmanager";
	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {
		/* START Test/Play with beanstalk API */
		String username = "brankoa";
		String host = "";
		String password = "MyPassHere";
		String urlBasePath = "http://" + username + "";
		String urlApiCall_FindAllRepositories = urlBasePath
				+ "repositories.xml";
		try {
			HttpClient client = new DefaultHttpClient();
			AuthScope as = new AuthScope(host, 443);
			UsernamePasswordCredentials upc = new UsernamePasswordCredentials(
					username, password);
			((AbstractHttpClient) client).getCredentialsProvider()
					.setCredentials(as, upc);
			BasicHttpContext localContext = new BasicHttpContext();
			BasicScheme basicAuth = new BasicScheme();
			localContext.setAttribute("preemptive-auth", basicAuth);
			HttpHost targetHost = new HttpHost(host, 443, "https");
			HttpGet httpget = new HttpGet(urlApiCall_FindAllRepositories);
			httpget.setHeader("Content-Type", "application/xml");
			HttpResponse response = client.execute(targetHost, httpget,
			HttpEntity entity = response.getEntity();
			Object content = EntityUtils.toString(entity);
			Log.d(MY_APP_TAG, "OK: " + content.toString());
		} catch (Exception e) {
			Log.d(MY_APP_TAG, "Error: " + e.getMessage());
		/* END Test/Play with beanstalk API */

Important thing to keep an eye on are Beanstalk specific account information, like username, password, your account url. So, if you plan on copy-pasting the code, please set those accordingly.

Also, response is shown on the screenshot below.

Basically I think the example alone speaks for it self. No need to get too deep into explaining things.

Hope it helps someone.

You made it all the way down here so you must have enjoyed this post! You may also like:

Android – custom UI look and feel Darko Goles
Darko Goles, | 2

Android – custom UI look and feel

Android ksoap2 and Magento v2 API Darko Goles
Darko Goles, | 46

Android ksoap2 and Magento v2 API

Magento API v2 SOAP demystified Darko Goles
Darko Goles, | 39

Magento API v2 SOAP demystified


  1. Can you please make a tutorial for Magento-Android REST API connection. I am suffering from a long time by trying to connect android to Magento REST Api, Thanks In Advance..

  2. First of all thx for your code and realization at all, can i ask you for help. This apache packages can do GET or POST or DEL request? Can i realize it for my requests?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.