Using java Syntax Highlighting
- package com.google.android.loginin;
- import android.app.Activity;
- import android.os.Bundle;
- 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 org.apache.http.util.ByteArrayBuffer;
- import android.net.http.EventHandler;
- import android.net.http.Headers;
- import android.net.http.RequestQueue;
- import android.net.http.SslCertificate;
- import android.widget.TextView;
- import com.google.android.loginin.globalclass;
- public class test2 extends Activity {
- TextView tv;
- globalclass GC;
- String text="";
- private final String DEBUG_TAG = "httpPostExample";
- /** Called when the activity is first created. */
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- android.net.http.RequestQueue rQueue = new RequestQueue(this);
- tv = new TextView(this);
- text.trim();
- /* Prepare the Post-Text we are going to send. */
- String POSTUid = "";
- String POSTPwd="";
- String POSTText="";
- StringBuilder b = new StringBuilder();
- try {
- b.append("UserId=");
- b.append(URLEncoder.encode(GC.uname,"UTF-8"));
- b.append("&");
- b.append("PassWd=");
- b.append(URLEncoder.encode(GC.pwd,"UTF-8"));
- POSTText=b.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");
- MyEventHandler myEvH = new MyEventHandler(this);
- rQueue.queueRequest("http://192.154.1.1:8080/star/index.jsp", "POST",
- headers,myEvH, baos, POSTbytes.length,false);
- /* Wait until the request is complete.*/
- rQueue.waitUntilComplete();
- setResult(1, text,null);
- finish();
- }
- private class MyEventHandler implements EventHandler {
- private static final int RANDOM_ID = 0x1337;
- /** 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.clear();
- baf.append(bytes, 0, len);}
- public void endData() {
- text = new String(baf.toByteArray());
- myShowNotificationAndLog(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) { }
- private void myShowNotificationAndLog(String msg) {
- int i=msg.indexOf("EXISTS");
- if(i==-1)
- text="INVALID";
- else
- text=msg;
- }
- }
- }
Parsed in 0.047 seconds, using GeSHi 1.0.8.4


