package org.anddev.android.testproject;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import android.app.Activity;
import android.app.ListActivity;
import android.app.NotificationManager;
import android.net.http.EventHandler;
import android.net.http.Headers;
import android.net.http.RequestQueue;
import android.net.http.SslCertificate;
import android.os.Bundle;
import android.util.Log;
public class TestLayout extends ListActivity {
class MyEventHandler implements EventHandler {
private ByteArrayOutputStream baos = new ByteArrayOutputStream();
private Activity activity;
MyEventHandler(Activity activity) {
this.activity = activity;
}
public void data(byte[] bytes, int len) {
baos.write(bytes, 0, len);
}
public void endData() {
String text = new String(baos.toByteArray());
Log.d("http", "load data [" + text + "]");
}
public void status(int arg0, int arg1, int arg2, String s) {
Log.d("http", "status [" + s + "]");
}
public void error(int i, String s) {
Log.d("http", "error [" + s + "]");
NotificationManager nm = (NotificationManager)
activity.getSystemService(Activity.NOTIFICATION_SERVICE);
nm.notifyWithText(0x1337,
"error [" + s + "]",
NotificationManager.LENGTH_SHORT, null);
}
public void handleSslErrorRequest(int arg0, String arg1, SslCertificate arg2) { }
public void headers(Iterator arg0) { }
public void headers(Headers arg0) { }
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
InputStream in = this.getResources().openRawResource(R.raw.everlast);
System.out.println(in.toString());
android.net.http.RequestQueue rQueue = new RequestQueue(this);
String POSTText;
try {
POSTText = "data=" + URLEncoder.encode("HELLO ANDROID", "UTF-8");
} catch (UnsupportedEncodingException e) {
return;
}
byte[] POSTbytes = POSTText.getBytes();
ByteArrayInputStream baos = new ByteArrayInputStream(POSTbytes);
Map<String,String> headers = new HashMap<String,String>();
headers.put("Content-Type", "application/x-www-form-urlencoded");
rQueue.queueRequest("http://www.anddev.org/postresponse.php", "POST", headers,
new MyEventHandler(this), baos, POSTbytes.length, false);
rQueue.waitUntilComplete();
}
}