package com.project.HTTPTRY;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.lang.String;
import org.apache.http.util.ByteArrayBuffer;
import android.app.Activity;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class httptry extends Activity {
// ===========================================================
// Fields
// ===========================================================
private EditText logE,passE;
private Button logB;
private final String DEBUG_TAG = "httpPostExample";
MyEventHandler myEvH = new MyEventHandler(this);
android.net.http.RequestQueue rQueue = new RequestQueue(this);
TextView tv = new TextView(this);
String POSTText = null;
StringBuilder strB=new StringBuilder();
// ===========================================================
// 'Constructors'
// ===========================================================
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
logB = (Button) findViewById(R.id.logButton);
logB.setOnClickListener(clickListener);
}
private OnClickListener clickListener = new OnClickListener() {
public void onClick(View v) {
logE=(EditText)findViewById(R.id.logEdit);
passE=(EditText)findViewById(R.id.passEdit);
try {
strB.append("UserId=");
strB.append(URLEncoder.encode(logE.getText().toString(),"UTF-8"));
strB.append("&");
strB.append("PassWd=");
strB.append(URLEncoder.encode(passE.getText().toString(),"UTF-8"));
POSTText=strB.toString();
} catch (UnsupportedEncodingException e) {
return;
}
/* And put the encoded bytes into an BAIS,
* where a function later can read bytes from. */
byte[] POSTbytes = POSTText.getBytes();
ByteArrayInputStream baos = new ByteArrayInputStream(POSTbytes);
/* Create a header-hashmap */
Map<String, String> headers = new HashMap<String, String>();
/* and put the Default-Encoding for html-forms to it. */
headers.put("Content-Type", "application/x-www-form-urlencoded");
/* Create a new EventHandler defined above, to handle what gets returned. */
// MyEventHandler myEvH = new MyEventHandler(this);
/* Now we call a php-file I prepared. It is exactly this:
* <?php
* echo "POSTed data: '".$_POST['data']."'";
* ?>*/
// rQueue.queueRequest("http://www.anddev.org/postresponse.php", "POST",
// headers, myEvH, baos, POSTbytes.length,false);
rQueue.queueRequest("http://www.mobiledumper.wsnw.net/httptry.php", "POST",
headers, myEvH, baos, POSTbytes.length,false);
/* Wait until the request is complete.*/
rQueue.waitUntilComplete();
tv.setText(myEvH.str);
setContentView(tv);
}
};
// ===========================================================
// Worker Class
// ===========================================================
private class MyEventHandler implements EventHandler {
private static final int RANDOM_ID = 0x1337;
String str;
/** Will hold the data returned by the URLCall. */
ByteArrayBuffer baf = new ByteArrayBuffer(20);
/** Needed, as we want to show the results as Notifications. */
private Activity myActivity;
MyEventHandler(Activity activity) {
this.myActivity = activity; }
public void data(byte[] bytes, int len) {
baf.append(bytes, 0, len); }
public void endData() {
String text = new String(baf.toByteArray());
// myShowNotificationAndLog("Data loaded: \n" + text);
str = new String(text);
}
public void status(int arg0, int arg1, int arg2, String s) {
// myShowNotificationAndLog("status [" + s + "]");
}
public void error(int i, String s) {
// this.myShowNotificationAndLog("error [" + s + "]");
}
public void handleSslErrorRequest(int arg0, String arg1, SslCertificate arg2) { }
public void headers(Iterator arg0) { }
public void headers(Headers arg0) { }
}
}