Android Programming Tutorials

(Romina) #1
Raising (Something Like) a Tweet

Step #5: Make the Status Post Request.............................................


With all that done, we can actually invoke the identi.ca REST API. This is


somewhat tedious, for a few reasons:



  • identi.ca requires HTTP Basic authentication to pass over the user


name and password, rather than embedding them as values in, say,
an HTTP POST request. HttpClient does not handle preemptive
HTTP authentication very well.


  • HTTP Basic authentication requires Base64 encoding, and Android
    (before 2.2) lacks a publicly-visible Base64 encoder.


Here is what you need to do to get past all of this and make Patchy work:


First, find a suitable Base64 encoder, such as this public domain one. Put


the Base64 class in your project in Patchy/src/apt/tutorial/two/ and add the


matching package line to the top of the file, so it is available to your Patchy


implementation. Note that Android 2.2 has a Base64 encoder class, though


that has not been tried as a part of this tutorial.


Next, replace your existing Patchy implementation with the following:


package apt.tutorial.two;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.HttpVersion;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

159
Free download pdf