Http POST Source (Sending Login Information to Server)

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Http POST Source (Sending Login Information to Server)

Postby CHAKYANAS » Tue Mar 18, 2008 11:39 am

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.google.android.loginin;
  2.  
  3. import android.app.Activity;
  4.  
  5. import android.os.Bundle;
  6.  
  7. import java.io.ByteArrayInputStream;
  8.  
  9. import java.io.UnsupportedEncodingException;
  10.  
  11. import java.net.URLEncoder;
  12.  
  13. import java.util.HashMap;
  14.  
  15. import java.util.Iterator;
  16.  
  17. import java.util.Map;
  18.  
  19. import org.apache.http.util.ByteArrayBuffer;
  20.  
  21.  
  22.  
  23. import android.net.http.EventHandler;
  24.  
  25. import android.net.http.Headers;
  26.  
  27. import android.net.http.RequestQueue;
  28.  
  29. import android.net.http.SslCertificate;
  30.  
  31. import android.widget.TextView;
  32.  
  33. import com.google.android.loginin.globalclass;
  34.  
  35.  
  36.  
  37. public class test2 extends Activity {
  38.  
  39.           TextView tv;
  40.  
  41.           globalclass GC;
  42.  
  43.           String text="";
  44.  
  45.          
  46.  
  47.           private final String DEBUG_TAG = "httpPostExample";
  48.  
  49.        
  50.  
  51.     /** Called when the activity is first created. */
  52.  
  53.    
  54.  
  55.     public void onCreate(Bundle icicle) {
  56.  
  57.         super.onCreate(icicle);
  58.  
  59.         android.net.http.RequestQueue rQueue = new RequestQueue(this);
  60.  
  61.         tv = new TextView(this);
  62.  
  63.         text.trim();
  64.  
  65.         /* Prepare the Post-Text we are going to send. */
  66.  
  67.         String POSTUid = "";
  68.  
  69.         String POSTPwd="";
  70.  
  71.         String POSTText="";
  72.  
  73.         StringBuilder b = new StringBuilder();
  74.  
  75.         try {
  76.  
  77.                 b.append("UserId=");
  78.  
  79.             b.append(URLEncoder.encode(GC.uname,"UTF-8"));
  80.  
  81.             b.append("&");
  82.  
  83.             b.append("PassWd=");
  84.  
  85.             b.append(URLEncoder.encode(GC.pwd,"UTF-8"));
  86.  
  87.             POSTText=b.toString();
  88.  
  89.                  }
  90.  
  91.         catch (UnsupportedEncodingException e) {
  92.  
  93.              return;
  94.  
  95.         }
  96.  
  97.         /* And put the encoded bytes into an BAIS,
  98.  
  99.          * where a function later can read bytes from. */
  100.  
  101.         byte[] POSTbytes = POSTText.getBytes();
  102.  
  103.         ByteArrayInputStream baos = new ByteArrayInputStream(POSTbytes);
  104.  
  105.        
  106.  
  107.         /* Create a header-hashmap */
  108.  
  109.         Map<String, String> headers = new HashMap<String, String>();
  110.  
  111.         /* and put the Default-Encoding for html-forms to it. */
  112.  
  113.         headers.put("Content-Type", "application/x-www-form-urlencoded");
  114.  
  115.         MyEventHandler myEvH = new MyEventHandler(this);
  116.  
  117.  
  118.  
  119.         rQueue.queueRequest("http://192.154.1.1:8080/star/index.jsp", "POST",
  120.  
  121.                   headers,myEvH, baos, POSTbytes.length,false);
  122.  
  123.         /* Wait until the request is complete.*/
  124.  
  125.        
  126.  
  127.         rQueue.waitUntilComplete();
  128.  
  129.         setResult(1, text,null);
  130.  
  131.         finish();
  132.  
  133.     }
  134.  
  135.    
  136.  
  137.     private class MyEventHandler implements EventHandler {
  138.  
  139.         private static final int RANDOM_ID = 0x1337;
  140.  
  141.         /** Will hold the data returned by the URLCall. */
  142.  
  143.         ByteArrayBuffer baf = new ByteArrayBuffer(20);
  144.  
  145.        
  146.  
  147.         /** Needed, as we want to show the results as Notifications. */
  148.  
  149.         private Activity myActivity;
  150.  
  151.         MyEventHandler(Activity activity) {
  152.  
  153.              this.myActivity = activity;  }
  154.  
  155.         public void data(byte[] bytes, int len) {
  156.  
  157.                 baf.clear();
  158.  
  159.              baf.append(bytes, 0, len);}
  160.  
  161.         public void endData() {
  162.  
  163.               text = new String(baf.toByteArray());
  164.  
  165.               myShowNotificationAndLog(text);
  166.  
  167.               }
  168.  
  169.         public void status(int arg0, int arg1, int arg2, String s) {
  170.  
  171.              myShowNotificationAndLog("status [" + s + "]");  }
  172.  
  173.         public void error(int i, String s) {
  174.  
  175.              this.myShowNotificationAndLog("error [" + s + "]");  }
  176.  
  177.         public void handleSslErrorRequest(int arg0, String arg1, SslCertificate arg2) { }
  178.  
  179.         public void headers(Iterator arg0) { }
  180.  
  181.         public void headers(Headers arg0) { }
  182.  
  183.         private void myShowNotificationAndLog(String msg) {
  184.  
  185.           int i=msg.indexOf("EXISTS");
  186.  
  187.           if(i==-1)
  188.  
  189.                   text="INVALID";
  190.  
  191.           else
  192.  
  193.               text=msg;
  194.  
  195.                  
  196.  
  197.          
  198.  
  199.    }
  200.  
  201.     }
  202.  
  203. }
Parsed in 0.047 seconds, using GeSHi 1.0.8.4
User avatar
CHAKYANAS
Developer
Developer
 
Posts: 44
Joined: Mon Mar 03, 2008 5:08 am
Location: INDIA

Top

Re: Http POST Source (Sending Login Information to Server)

Postby rohan » Sun Mar 23, 2008 6:19 am

CHAKYANAS wrote:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.google.android.loginin;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import java.io.ByteArrayInputStream;
  5. import java.io.UnsupportedEncodingException;
  6. import java.net.URLEncoder;
  7. import java.util.HashMap;
  8. import java.util.Iterator;
  9. import java.util.Map;
  10. import org.apache.http.util.ByteArrayBuffer;
  11.  
  12. import android.net.http.EventHandler;
  13. import android.net.http.Headers;
  14. import android.net.http.RequestQueue;
  15. import android.net.http.SslCertificate;
  16. import android.widget.TextView;
  17. import com.google.android.loginin.globalclass;
  18.  
  19. public class test2 extends Activity {
  20.           TextView tv;
  21.           globalclass GC;
  22.           String text="";
  23.          
  24.           private final String DEBUG_TAG = "httpPostExample";
  25.        
  26.     /** Called when the activity is first created. */
  27.    
  28.     public void onCreate(Bundle icicle) {
  29.         super.onCreate(icicle);
  30.         android.net.http.RequestQueue rQueue = new RequestQueue(this);
  31.         tv = new TextView(this);
  32.         text.trim();
  33.         /* Prepare the Post-Text we are going to send. */
  34.         String POSTUid = "";
  35.         String POSTPwd="";
  36.         String POSTText="";
  37.         StringBuilder b = new StringBuilder();
  38.         try {
  39.                 b.append("UserId=");
  40.             b.append(URLEncoder.encode(GC.uname,"UTF-8"));
  41.             b.append("&");
  42.             b.append("PassWd=");
  43.             b.append(URLEncoder.encode(GC.pwd,"UTF-8"));
  44.             POSTText=b.toString();
  45.                  }
  46.         catch (UnsupportedEncodingException e) {
  47.              return;
  48.         }
  49.         /* And put the encoded bytes into an BAIS,
  50.          * where a function later can read bytes from. */
  51.         byte[] POSTbytes = POSTText.getBytes();
  52.         ByteArrayInputStream baos = new ByteArrayInputStream(POSTbytes);
  53.        
  54.         /* Create a header-hashmap */
  55.         Map<String, String> headers = new HashMap<String, String>();
  56.         /* and put the Default-Encoding for html-forms to it. */
  57.         headers.put("Content-Type", "application/x-www-form-urlencoded");
  58.         MyEventHandler myEvH = new MyEventHandler(this);
  59.  
  60.         rQueue.queueRequest("http://192.154.1.1:8080/star/index.jsp", "POST",
  61.                   headers,myEvH, baos, POSTbytes.length,false);
  62.         /* Wait until the request is complete.*/
  63.        
  64.         rQueue.waitUntilComplete();
  65.         setResult(1, text,null);
  66.         finish();
  67.     }
  68.    
  69.     private class MyEventHandler implements EventHandler {
  70.         private static final int RANDOM_ID = 0x1337;
  71.         /** Will hold the data returned by the URLCall. */
  72.         ByteArrayBuffer baf = new ByteArrayBuffer(20);
  73.        
  74.         /** Needed, as we want to show the results as Notifications. */
  75.         private Activity myActivity;
  76.         MyEventHandler(Activity activity) {
  77.              this.myActivity = activity;  }
  78.         public void data(byte[] bytes, int len) {
  79.                 baf.clear();
  80.              baf.append(bytes, 0, len);}
  81.         public void endData() {
  82.               text = new String(baf.toByteArray());
  83.               myShowNotificationAndLog(text);
  84.               }
  85.         public void status(int arg0, int arg1, int arg2, String s) {
  86.              myShowNotificationAndLog("status [" + s + "]");  }
  87.         public void error(int i, String s) {
  88.              this.myShowNotificationAndLog("error [" + s + "]");  }
  89.         public void handleSslErrorRequest(int arg0, String arg1, SslCertificate arg2) { }
  90.         public void headers(Iterator arg0) { }
  91.         public void headers(Headers arg0) { }
  92.         private void myShowNotificationAndLog(String msg) {
  93.           int i=msg.indexOf("EXISTS");
  94.           if(i==-1)
  95.                   text="INVALID";
  96.           else
  97.               text=msg;
  98.                  
  99.          
  100.    }
  101.     }
  102. }
Parsed in 0.046 seconds, using GeSHi 1.0.8.4


hi CHAKYANAS,
i m not gettin wht "import com.google.android.loginin.globalclass;" globalclass is ?
can u give the complete source code.....
OR the simplest code to try http.......
thanx
rohan
Developer
Developer
 
Posts: 34
Joined: Wed Mar 19, 2008 2:26 pm

Postby CHAKYANAS » Sun Mar 23, 2008 3:17 pm

HI rohan...............

globalclass is simply a java class .... its store global varible(all the variable that is shared among different classes)


For Example....In my application first page consists of entering usename and password from the user and after hitting the submit button it has to validate it in the server .....So this user name and password i am validating in another class(source code i posted) So from the first page the user name and password should be stored in variables(varialbles in globalclass) so that i can take later in my code................

or you can use the global varialbes in the same class where the user is entering username and password
I hope you understood................................................


import com.google.android.loginin.globalclass;
just importing the global class where my shared variables are stored ........................
User avatar
CHAKYANAS
Developer
Developer
 
Posts: 44
Joined: Mon Mar 03, 2008 5:08 am
Location: INDIA

Postby CHAKYANAS » Sun Mar 23, 2008 3:21 pm

HI ROHAN......


Most of the codes inside class MyEventHandler are default(must) if u avoid one of the lines it lll show you Error


If you have any doubt please sent me reply


best of luck

chakyanas :) :) :) :) :) :) :) :) :) :) :) :)
User avatar
CHAKYANAS
Developer
Developer
 
Posts: 44
Joined: Mon Mar 03, 2008 5:08 am
Location: INDIA

Postby CHAKYANAS » Sun Mar 23, 2008 3:30 pm

Hi Rohan ,,,,,
Simple httppost code unwanted code removed


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.google.android.loginin;
  2.  
  3. import android.app.Activity;
  4.  
  5. import android.os.Bundle;
  6.  
  7. import java.io.ByteArrayInputStream;
  8.  
  9. import java.io.UnsupportedEncodingException;
  10.  
  11. import java.net.URLEncoder;
  12.  
  13. import java.util.HashMap;
  14.  
  15. import java.util.Iterator;
  16.  
  17. import java.util.Map;
  18.  
  19. import org.apache.http.util.ByteArrayBuffer;
  20.  
  21.  
  22.  
  23. import android.net.http.EventHandler;
  24.  
  25. import android.net.http.Headers;
  26.  
  27. import android.net.http.RequestQueue;
  28.  
  29. import android.net.http.SslCertificate;
  30.  
  31. import android.widget.TextView;
  32.  
  33. import com.google.android.loginin.globalclass;
  34.  
  35.  
  36.  
  37. public class test2 extends Activity {
  38.  
  39.        TextView tv;
  40.  
  41.        globalclass GC;
  42.  
  43.        String text="";
  44.  
  45.        
  46.  
  47.  
  48.  
  49.      
  50.  
  51.     /** Called when the activity is first created. */
  52.  
  53.    
  54.  
  55.     public void onCreate(Bundle icicle) {
  56.  
  57.         super.onCreate(icicle);
  58.  
  59.         android.net.http.RequestQueue rQueue = new RequestQueue(this);
  60.  
  61.  
  62.  
  63.      
  64.  
  65.         /* Prepare the Post-Text we are going to send. */
  66.  
  67.        
  68.  
  69.         StringBuilder b = new StringBuilder();
  70.  
  71.         try {
  72.  
  73.           b.append("UserId=");
  74.  
  75.             b.append(URLEncoder.encode(GC.uname,"UTF-8"));
  76.  
  77.             b.append("&");
  78.  
  79.             b.append("PassWd=");
  80.  
  81.             b.append(URLEncoder.encode(GC.pwd,"UTF-8"));
  82.  
  83.             POSTText=b.toString();
  84.  
  85.            }
  86.  
  87.         catch (UnsupportedEncodingException e) {
  88.  
  89.              return;
  90.  
  91.         }
  92.  
  93.         /* And put the encoded bytes into an BAIS,
  94.  
  95.          * where a function later can read bytes from. */
  96.  
  97.         byte[] POSTbytes = POSTText.getBytes();
  98.  
  99.         ByteArrayInputStream baos = new ByteArrayInputStream(POSTbytes);
  100.  
  101.        
  102.  
  103.         /* Create a header-hashmap */
  104.  
  105.         Map<String, String> headers = new HashMap<String, String>();
  106.  
  107.         /* and put the Default-Encoding for html-forms to it. */
  108.  
  109.         headers.put("Content-Type", "application/x-www-form-urlencoded");
  110.  
  111.         MyEventHandler myEvH = new MyEventHandler(this);
  112.  
  113.  
  114.  
  115.         rQueue.queueRequest("http://192.154.1.1:8080/star/index.jsp", "POST",
  116.  
  117.                   headers,myEvH, baos, POSTbytes.length,false);
  118.  
  119.         /* Wait until the request is complete.*/
  120.  
  121.        
  122.  
  123.         rQueue.waitUntilComplete();
  124.  
  125.  
  126.  
  127. //returning the result back to the parent activity
  128.  
  129.         setResult(1, text,null);
  130.  
  131.         finish();
  132.  
  133.     }
  134.  
  135.    
  136.  
  137.     private class MyEventHandler implements EventHandler {
  138.  
  139.         private static final int RANDOM_ID = 0x1337;
  140.  
  141.         /** Will hold the data returned by the URLCall. */
  142.  
  143.         ByteArrayBuffer baf = new ByteArrayBuffer(20);
  144.  
  145.        
  146.  
  147.         /** Needed, as we want to show the results as Notifications. */
  148.  
  149.         private Activity myActivity;
  150.  
  151.         MyEventHandler(Activity activity) {
  152.  
  153.              this.myActivity = activity;  }
  154.  
  155.         public void data(byte[] bytes, int len) {
  156.  
  157.                   baf.clear();
  158.  
  159.              baf.append(bytes, 0, len);}
  160.  
  161.         public void endData() {
  162.  
  163.               text = new String(baf.toByteArray());
  164.  
  165.               myShowNotificationAndLog(text);
  166.  
  167.               }
  168.  
  169.         public void status(int arg0, int arg1, int arg2, String s) {
  170.  
  171.              myShowNotificationAndLog("status [" + s + "]");  }
  172.  
  173.         public void error(int i, String s) {
  174.  
  175.              this.myShowNotificationAndLog("error [" + s + "]");  }
  176.  
  177.         public void handleSslErrorRequest(int arg0, String arg1, SslCertificate arg2) { }
  178.  
  179.         public void headers(Iterator arg0) { }
  180.  
  181.         public void headers(Headers arg0) { }
  182.  
  183.         private void myShowNotificationAndLog(String msg) {
  184.  
  185. //preparing text to return text to the parent activity
  186.  
  187.           int i=msg.indexOf("EXISTS");
  188.  
  189.           if(i==-1)
  190.  
  191.             text="INVALID";
  192.  
  193.           else
  194.  
  195.               text=msg;
  196.  
  197.            
  198.  
  199.          
  200.  
  201.    }
  202.  
  203.     }
  204.  
  205. }
Parsed in 0.048 seconds, using GeSHi 1.0.8.4
User avatar
CHAKYANAS
Developer
Developer
 
Posts: 44
Joined: Mon Mar 03, 2008 5:08 am
Location: INDIA

Postby CHAKYANAS » Sun Mar 23, 2008 3:33 pm

Hi Rihan ,,,


Onething you have to notice that all the validation are done at the server jsp page....

and the server will return only text (EXISTS OR INVALID)


which we are getting in the code

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  public void data(byte[] bytes, int len) {
  2.  
  3. baf.clear();
  4.  
  5. baf.append(bytes, 0, len);}
Parsed in 0.033 seconds, using GeSHi 1.0.8.4




Happy Coding
:lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol:
User avatar
CHAKYANAS
Developer
Developer
 
Posts: 44
Joined: Mon Mar 03, 2008 5:08 am
Location: INDIA

Top

Postby rohan » Sun Mar 23, 2008 5:16 pm

CHAKYANAS wrote:Hi Rohan ,,,,,
Simple httppost code unwanted code removed


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.google.android.loginin;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import java.io.ByteArrayInputStream;
  5. import java.io.UnsupportedEncodingException;
  6. import java.net.URLEncoder;
  7. import java.util.HashMap;
  8. import java.util.Iterator;
  9. import java.util.Map;
  10. import org.apache.http.util.ByteArrayBuffer;
  11.  
  12. import android.net.http.EventHandler;
  13. import android.net.http.Headers;
  14. import android.net.http.RequestQueue;
  15. import android.net.http.SslCertificate;
  16. import android.widget.TextView;
  17. import com.google.android.loginin.globalclass;
  18.  
  19. public class test2 extends Activity {
  20.        TextView tv;
  21.        globalclass GC;
  22.        String text="";
  23.        
  24.  
  25.      
  26.     /** Called when the activity is first created. */
  27.    
  28.     public void onCreate(Bundle icicle) {
  29.         super.onCreate(icicle);
  30.         android.net.http.RequestQueue rQueue = new RequestQueue(this);
  31.  
  32.      
  33.         /* Prepare the Post-Text we are going to send. */
  34.        
  35.         StringBuilder b = new StringBuilder();
  36.         try {
  37.           b.append("UserId=");
  38.             b.append(URLEncoder.encode(GC.uname,"UTF-8"));
  39.             b.append("&");
  40.             b.append("PassWd=");
  41.             b.append(URLEncoder.encode(GC.pwd,"UTF-8"));
  42.             POSTText=b.toString();
  43.            }
  44.         catch (UnsupportedEncodingException e) {
  45.              return;
  46.         }
  47.         /* And put the encoded bytes into an BAIS,
  48.          * where a function later can read bytes from. */
  49.         byte[] POSTbytes = POSTText.getBytes();
  50.         ByteArrayInputStream baos = new ByteArrayInputStream(POSTbytes);
  51.        
  52.         /* Create a header-hashmap */
  53.         Map<String, String> headers = new HashMap<String, String>();
  54.         /* and put the Default-Encoding for html-forms to it. */
  55.         headers.put("Content-Type", "application/x-www-form-urlencoded");
  56.         MyEventHandler myEvH = new MyEventHandler(this);
  57.  
  58.         rQueue.queueRequest("http://192.154.1.1:8080/star/index.jsp", "POST",
  59.                   headers,myEvH, baos, POSTbytes.length,false);
  60.         /* Wait until the request is complete.*/
  61.        
  62.         rQueue.waitUntilComplete();
  63.  
  64. //returning the result back to the parent activity
  65.         setResult(1, text,null);
  66.         finish();
  67.     }
  68.    
  69.     private class MyEventHandler implements EventHandler {
  70.         private static final int RANDOM_ID = 0x1337;
  71.         /** Will hold the data returned by the URLCall. */
  72.         ByteArrayBuffer baf = new ByteArrayBuffer(20);
  73.        
  74.         /** Needed, as we want to show the results as Notifications. */
  75.         private Activity myActivity;
  76.         MyEventHandler(Activity activity) {
  77.              this.myActivity = activity;  }
  78.         public void data(byte[] bytes, int len) {
  79.                   baf.clear();
  80.              baf.append(bytes, 0, len);}
  81.         public void endData() {
  82.               text = new String(baf.toByteArray());
  83.               myShowNotificationAndLog(text);
  84.               }
  85.         public void status(int arg0, int arg1, int arg2, String s) {
  86.              myShowNotificationAndLog("status [" + s + "]");  }
  87.         public void error(int i, String s) {
  88.              this.myShowNotificationAndLog("error [" + s + "]");  }
  89.         public void handleSslErrorRequest(int arg0, String arg1, SslCertificate arg2) { }
  90.         public void headers(Iterator arg0) { }
  91.         public void headers(Headers arg0) { }
  92.         private void myShowNotificationAndLog(String msg) {
  93. //preparing text to return text to the parent activity
  94.           int i=msg.indexOf("EXISTS");
  95.           if(i==-1)
  96.             text="INVALID";
  97.           else
  98.               text=msg;
  99.            
  100.          
  101.    }
  102.     }
  103. }
Parsed in 0.051 seconds, using GeSHi 1.0.8.4


thanx CHAKYANAS,
I have used the code the plusminius had posted. But nw i want to display the message that server sends to my application using TextView instead of showing notification...........
plz help me,
Thanx.........
rohan
Developer
Developer
 
Posts: 34
Joined: Wed Mar 19, 2008 2:26 pm

Postby CHAKYANAS » Sun Mar 23, 2008 7:08 pm

HI Rohan,,,,,,,,,,,,,


this is the code(buffer) where u r receiving any result fom the server

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void data(byte[] bytes, int len) {
  2.  
  3. baf.clear();
  4.  
  5. baf.append(bytes, 0, len);}
Parsed in 0.034 seconds, using GeSHi 1.0.8.4



So if you have something like this in the code it ll work fine

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void endData() {
  2.  
  3. text = new String(baf.toByteArray());
  4.  
  5. tv.settext(text);
  6.  
  7. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4



this code ll work fine assuming tv is the TextView.......................................


Best Of Luck............
:lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol:
User avatar
CHAKYANAS
Developer
Developer
 
Posts: 44
Joined: Mon Mar 03, 2008 5:08 am
Location: INDIA

Postby rohan » Mon Mar 24, 2008 8:26 am

Thanx CHAKYANAS,
I have tried using TextView bt its nt workin. I dont knw where to declare TextView tv=new TextView(this); and where to use setContentView(tv);. When i give setContentView(tv) in notification method it gives me nullpointerexception. Tel me where to declare TextView and use setContentView(tv) to display username and password onto screen.

Rohan.
rohan
Developer
Developer
 
Posts: 34
Joined: Wed Mar 19, 2008 2:26 pm

Postby CHAKYANAS » Mon Mar 24, 2008 9:13 am

Hi Rohan,,,


I hope u understood where u have to write code for

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. string text = new String(baf.toByteArray());
  2.  
  3. tv.settext(text);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


ok....................

textView must be declared on main()

or either in xml layout


if u r specifying in xml you should specify android:id instead of id


if u r declaring textview in java file


you call setcontentview (tv) after
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. rQueue.queueRequest("http://192.154.1.1:8080/star/index.jsp", "POST",
  3.  
  4. headers,myEvH, baos, POSTbytes.length,false);
  5.  
  6. /* Wait until the request is complete.*/
  7.  
  8.  
  9.  
  10. rQueue.waitUntilComplete();
  11.  
  12.  
  13.  
  14. setContentView(tv)
Parsed in 0.036 seconds, using GeSHi 1.0.8.4



if its not working plz sent your source code to me................
User avatar
CHAKYANAS
Developer
Developer
 
Posts: 44
Joined: Mon Mar 03, 2008 5:08 am
Location: INDIA

Postby rohan » Mon Mar 24, 2008 9:25 am

thanx CHAKYANAS,
this is my code.....plz edit and send ......using this code its givin me nullpointerexcption

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.project.HTTPTRY;
  2.  
  3.  
  4.  
  5. import java.io.ByteArrayInputStream;
  6.  
  7. import java.io.UnsupportedEncodingException;
  8.  
  9. import java.net.URLEncoder;
  10.  
  11. import java.util.HashMap;
  12.  
  13. import java.util.Iterator;
  14.  
  15. import java.util.Map;
  16.  
  17.  
  18.  
  19. import org.apache.http.util.ByteArrayBuffer;
  20.  
  21.  
  22.  
  23. import android.app.Activity;
  24.  
  25. import android.app.NotificationManager;
  26.  
  27. import android.net.http.EventHandler;
  28.  
  29. import android.net.http.Headers;
  30.  
  31. import android.net.http.RequestQueue;
  32.  
  33. import android.net.http.SslCertificate;
  34.  
  35. import android.os.Bundle;
  36.  
  37. import android.util.Log;
  38.  
  39. import android.widget.TextView;
  40.  
  41.  
  42.  
  43.  
  44.  
  45. public class httptry extends Activity {
  46.  
  47.  
  48.  
  49.      // ===========================================================
  50.  
  51.      // Fields
  52.  
  53.      // ===========================================================
  54.  
  55.      
  56.  
  57.      private final String DEBUG_TAG = "httpPostExample";
  58.  
  59.    
  60.  
  61.      // ===========================================================
  62.  
  63.      // 'Constructors'
  64.  
  65.      // ===========================================================
  66.  
  67.      public TextView tv=new TextView(this);
  68.  
  69.      @Override
  70.  
  71.      public void onCreate(Bundle icicle) {
  72.  
  73.           super.onCreate(icicle);
  74.  
  75.        
  76.  
  77.           /* Create a new HTTP-RequestQueue. */
  78.  
  79.           android.net.http.RequestQueue rQueue = new RequestQueue(this);
  80.  
  81.          
  82.  
  83.           /* Prepare the Post-Text we are going to send. */
  84.  
  85.           String POSTText = null;
  86.  
  87.           try {
  88.  
  89.                POSTText = "mydata=" + URLEncoder.encode("Mobiledumper rocks!!!", "UTF-8");
  90.  
  91.           } catch (UnsupportedEncodingException e) {
  92.  
  93.                return;
  94.  
  95.           }
  96.  
  97.           /* And put the encoded bytes into an BAIS,
  98.  
  99.            * where a function later can read bytes from. */
  100.  
  101.           byte[] POSTbytes = POSTText.getBytes();
  102.  
  103.           ByteArrayInputStream baos = new ByteArrayInputStream(POSTbytes);
  104.  
  105.          
  106.  
  107.           /* Create a header-hashmap */
  108.  
  109.           Map<String, String> headers = new HashMap<String, String>();
  110.  
  111.           /* and put the Default-Encoding for html-forms to it. */
  112.  
  113.           headers.put("Content-Type", "application/x-www-form-urlencoded");
  114.  
  115.          
  116.  
  117.           /* Create a new EventHandler defined above, to handle what gets returned. */
  118.  
  119.           MyEventHandler myEvH = new MyEventHandler(this);
  120.  
  121.  
  122.  
  123.           /* Now we call a php-file I prepared. It is exactly this:
  124.  
  125.            * <?php
  126.  
  127.            *        echo "POSTed data: '".$_POST['data']."'";
  128.  
  129.            * ?>*/
  130.  
  131.          // rQueue.queueRequest("http://www.anddev.org/postresponse.php", "POST",
  132.  
  133.          //          headers, myEvH, baos, POSTbytes.length,false);
  134.  
  135.           rQueue.queueRequest("http://www.mobiledumper.wsnw.net/httptry.php", "POST",
  136.  
  137.                   headers, myEvH, baos, POSTbytes.length,false);
  138.  
  139.  
  140.  
  141.           /* Wait until the request is complete.*/
  142.  
  143.           rQueue.waitUntilComplete();
  144.  
  145.           setContentView(tv);
  146.  
  147.      }
  148.  
  149.      
  150.  
  151.      // ===========================================================
  152.  
  153.      // Worker Class
  154.  
  155.      // ===========================================================
  156.  
  157.      
  158.  
  159.      private class MyEventHandler implements EventHandler {
  160.  
  161.           private static final int RANDOM_ID = 0x1337;
  162.  
  163.      
  164.  
  165.           /** Will hold the data returned by the URLCall. */
  166.  
  167.           ByteArrayBuffer baf = new ByteArrayBuffer(20);
  168.  
  169.          
  170.  
  171.           /** Needed, as we want to show the results as Notifications. */
  172.  
  173.           private Activity myActivity;
  174.  
  175.  
  176.  
  177.           MyEventHandler(Activity activity) {
  178.  
  179.                this.myActivity = activity;  }
  180.  
  181.  
  182.  
  183.           public void data(byte[] bytes, int len) {
  184.  
  185.                baf.append(bytes, 0, len);  }
  186.  
  187.  
  188.  
  189.           public void endData() {
  190.  
  191.                String text = new String(baf.toByteArray());
  192.  
  193.             //   myShowNotificationAndLog("Data loaded: \n" + text);
  194.  
  195.                tv.setText(text);
  196.  
  197.            
  198.  
  199.                }
  200.  
  201.          
  202.  
  203.           public void status(int arg0, int arg1, int arg2, String s) {
  204.  
  205.               // myShowNotificationAndLog("status [" + s + "]");
  206.  
  207.                   }
  208.  
  209.  
  210.  
  211.           public void error(int i, String s) {
  212.  
  213.               // this.myShowNotificationAndLog("error [" + s + "]");  
  214.  
  215.                   }
  216.  
  217.  
  218.  
  219.           public void handleSslErrorRequest(int arg0, String arg1, SslCertificate arg2) { }
  220.  
  221.           public void headers(Iterator arg0) { }
  222.  
  223.           public void headers(Headers arg0) { }
  224.  
  225.  
  226.  
  227.           private void myShowNotificationAndLog(String msg) {
  228.  
  229.                /* Print msg to LogCat and show Notification. */
  230.  
  231.                  
  232.  
  233.               /*Log.d(DEBUG_TAG, msg);
  234.  
  235.                NotificationManager nm = (NotificationManager) this.myActivity
  236.  
  237.                          .getSystemService(Activity.NOTIFICATION_SERVICE);
  238.  
  239.                nm.notifyWithText(RANDOM_ID, msg, NotificationManager.LENGTH_LONG, null);*/
  240.  
  241.                
  242.  
  243.                  
  244.  
  245.           }
  246.  
  247.          
  248.  
  249.      }
  250.  
  251.      
  252.  
  253. }
Parsed in 0.055 seconds, using GeSHi 1.0.8.4



Rohan .
rohan
Developer
Developer
 
Posts: 34
Joined: Wed Mar 19, 2008 2:26 pm

Postby CHAKYANAS » Mon Mar 24, 2008 9:33 am

hi rohan,,,


you just try to set setText("HELLO");


and run the apps
check it out whether its working or not?????
User avatar
CHAKYANAS
Developer
Developer
 
Posts: 44
Joined: Mon Mar 03, 2008 5:08 am
Location: INDIA

Postby rohan » Mon Mar 24, 2008 9:43 am

Thanx CHAKYANAS,
This is the code whch is perfectly working .........thanx for ur reply.... and I have one more thing , from my server how should i send separate messages and in my app hw shld i break those messages ....

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.project.HTTPTRY;
  2.  
  3.  
  4.  
  5. import java.io.ByteArrayInputStream;
  6.  
  7. import java.io.UnsupportedEncodingException;
  8.  
  9. import java.net.URLEncoder;
  10.  
  11. import java.util.HashMap;
  12.  
  13. import java.util.Iterator;
  14.  
  15. import java.util.Map;
  16.  
  17.  
  18.  
  19. import org.apache.http.util.ByteArrayBuffer;
  20.  
  21.  
  22.  
  23. import android.app.Activity;
  24.  
  25. import android.app.NotificationManager;
  26.  
  27. import android.net.http.EventHandler;
  28.  
  29. import android.net.http.Headers;
  30.  
  31. import android.net.http.RequestQueue;
  32.  
  33. import android.net.http.SslCertificate;
  34.  
  35. import android.os.Bundle;
  36.  
  37. import android.util.Log;
  38.  
  39. import android.widget.TextView;
  40.  
  41.  
  42.  
  43.  
  44.  
  45. public class httptry extends Activity {
  46.  
  47.  
  48.  
  49.      // ===========================================================
  50.  
  51.      // Fields
  52.  
  53.      // ===========================================================
  54.  
  55.      
  56.  
  57.      private final String DEBUG_TAG = "httpPostExample";
  58.  
  59.    
  60.  
  61.      // ===========================================================
  62.  
  63.      // 'Constructors'
  64.  
  65.      // ===========================================================
  66.  
  67.    
  68.  
  69.      @Override
  70.  
  71.      public void onCreate(Bundle icicle) {
  72.  
  73.           super.onCreate(icicle);
  74.  
  75.        
  76.  
  77.           /* Create a new HTTP-RequestQueue. */
  78.  
  79.           android.net.http.RequestQueue rQueue = new RequestQueue(this);
  80.  
  81.          
  82.  
  83.           /* Prepare the Post-Text we are going to send. */
  84.  
  85.           String POSTText = null;
  86.  
  87.           try {
  88.  
  89.                POSTText = "mydata=" + URLEncoder.encode("Mobiledumper rocks!!!", "UTF-8");
  90.  
  91.           } catch (UnsupportedEncodingException e) {
  92.  
  93.                return;
  94.  
  95.           }
  96.  
  97.           /* And put the encoded bytes into an BAIS,
  98.  
  99.            * where a function later can read bytes from. */
  100.  
  101.           byte[] POSTbytes = POSTText.getBytes();
  102.  
  103.           ByteArrayInputStream baos = new ByteArrayInputStream(POSTbytes);
  104.  
  105.          
  106.  
  107.           /* Create a header-hashmap */
  108.  
  109.           Map<String, String> headers = new HashMap<String, String>();
  110.  
  111.           /* and put the Default-Encoding for html-forms to it. */
  112.  
  113.           headers.put("Content-Type", "application/x-www-form-urlencoded");
  114.  
  115.          
  116.  
  117.           /* Create a new EventHandler defined above, to handle what gets returned. */
  118.  
  119.           MyEventHandler myEvH = new MyEventHandler(this);
  120.  
  121.  
  122.  
  123.           /* Now we call a php-file I prepared. It is exactly this:
  124.  
  125.            * <?php
  126.  
  127.            *        echo "POSTed data: '".$_POST['data']."'";
  128.  
  129.            * ?>*/
  130.  
  131.          // rQueue.queueRequest("http://www.anddev.org/postresponse.php", "POST",
  132.  
  133.          //          headers, myEvH, baos, POSTbytes.length,false);
  134.  
  135.           rQueue.queueRequest("http://www.mobiledumper.wsnw.net/httptry.php", "POST",
  136.  
  137.                   headers, myEvH, baos, POSTbytes.length,false);
  138.  
  139.  
  140.  
  141.           /* Wait until the request is complete.*/
  142.  
  143.           rQueue.waitUntilComplete();
  144.  
  145.           TextView tv = new TextView(this);
  146.  
  147.           tv.setText(myEvH.str);
  148.  
  149.           setContentView(tv);
  150.  
  151.      }
  152.  
  153.      
  154.  
  155.      // ===========================================================
  156.  
  157.      // Worker Class
  158.  
  159.      // ===========================================================
  160.  
  161.      
  162.  
  163.      private class MyEventHandler implements EventHandler {
  164.  
  165.           private static final int RANDOM_ID = 0x1337;
  166.  
  167.           String str;
  168.  
  169.           /** Will hold the data returned by the URLCall. */
  170.  
  171.           ByteArrayBuffer baf = new ByteArrayBuffer(20);
  172.  
  173.          
  174.  
  175.           /** Needed, as we want to show the results as Notifications. */
  176.  
  177.           private Activity myActivity;
  178.  
  179.  
  180.  
  181.           MyEventHandler(Activity activity) {
  182.  
  183.                this.myActivity = activity;  }
  184.  
  185.  
  186.  
  187.           public void data(byte[] bytes, int len) {
  188.  
  189.                baf.append(bytes, 0, len);  }
  190.  
  191.  
  192.  
  193.           public void endData() {
  194.  
  195.                String text = new String(baf.toByteArray());
  196.  
  197.             //   myShowNotificationAndLog("Data loaded: \n" + text);
  198.  
  199.                str = new String(text);
  200.  
  201.            
  202.  
  203.                }
  204.  
  205.          
  206.  
  207.           public void status(int arg0, int arg1, int arg2, String s) {
  208.  
  209.               // myShowNotificationAndLog("status [" + s + "]");
  210.  
  211.                   }
  212.  
  213.  
  214.  
  215.           public void error(int i, String s) {
  216.  
  217.               // this.myShowNotificationAndLog("error [" + s + "]");  
  218.  
  219.                   }
  220.  
  221.  
  222.  
  223.           public void handleSslErrorRequest(int arg0, String arg1, SslCertificate arg2) { }
  224.  
  225.           public void headers(Iterator arg0) { }
  226.  
  227.           public void headers(Headers arg0) { }
  228.  
  229.  
  230.  
  231.           private void myShowNotificationAndLog(String msg) {
  232.  
  233.                /* Print msg to LogCat and show Notification. */
  234.  
  235.                  
  236.  
  237.               Log.d(DEBUG_TAG, msg);
  238.  
  239.                NotificationManager nm = (NotificationManager) this.myActivity
  240.  
  241.                          .getSystemService(Activity.NOTIFICATION_SERVICE);
  242.  
  243.                nm.notifyWithText(RANDOM_ID, msg, NotificationManager.LENGTH_LONG, null);
  244.  
  245.                
  246.  
  247.                  
  248.  
  249.           }
  250.  
  251.          
  252.  
  253.      }
  254.  
  255.      
  256.  
  257. }
  258.  
  259.  
Parsed in 0.054 seconds, using GeSHi 1.0.8.4


Thanx,
Rohan .
rohan
Developer
Developer
 
Posts: 34
Joined: Wed Mar 19, 2008 2:26 pm

Postby CHAKYANAS » Mon Mar 24, 2008 10:08 am

Good News...................Rohan :lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol:
User avatar
CHAKYANAS
Developer
Developer
 
Posts: 44
Joined: Mon Mar 03, 2008 5:08 am
Location: INDIA

Postby rohan » Mon Mar 24, 2008 12:46 pm

Hi Chakyanas,
I have created a file in which user has to enter his username and password ,after submitting the data my app connects to server and sends the same data to the app. See this code,(its not working ,its givin me nullpointerexception). plz help

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) { }



}

}


Rohan .
rohan
Developer
Developer
 
Posts: 34
Joined: Wed Mar 19, 2008 2:26 pm

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 8 guests