HTTP Post, Web Services in Android

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

HTTP Post, Web Services in Android

Postby squid » Mon Dec 17, 2007 6:30 am

Hello,
Does anyone know how to do http post and web service in android? It would be great if we could have a tutorial on this topic.
Thanks,
Squid
squid
Freshman
Freshman
 
Posts: 2
Joined: Mon Dec 17, 2007 6:22 am

Top

Postby plusminus » Mon Dec 17, 2007 9:23 am

Hello squid,

I was trying that right now but not succeeding. :|

I put a php-File to the Server:
Syntax: [ Download ] [ Hide ]
Using php Syntax Highlighting
  1. <?php
  2.  
  3.         echo "POSTed data: '".$_POST['data']."'<br />";
  4.  
  5.         echo "GETed data: '".$_GET['data']."'";
  6.  
  7. ?>
Parsed in 0.054 seconds, using GeSHi 1.0.8.4


This is my code to access it (no time for comments xD):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.testproject;
  2.  
  3.  
  4.  
  5. import java.io.ByteArrayInputStream;
  6.  
  7. import java.io.ByteArrayOutputStream;
  8.  
  9. import java.io.InputStream;
  10.  
  11. import java.io.UnsupportedEncodingException;
  12.  
  13. import java.net.URLEncoder;
  14.  
  15. import java.util.Iterator;
  16.  
  17.  
  18.  
  19. import android.app.Activity;
  20.  
  21. import android.app.ListActivity;
  22.  
  23. import android.app.NotificationManager;
  24.  
  25. import android.net.http.EventHandler;
  26.  
  27. import android.net.http.Headers;
  28.  
  29. import android.net.http.RequestQueue;
  30.  
  31. import android.net.http.SslCertificate;
  32.  
  33. import android.os.Bundle;
  34.  
  35. import android.telephony.PhoneStateIntentReceiver;
  36.  
  37. import android.util.Log;
  38.  
  39.  
  40.  
  41. public class TestLayout extends ListActivity {
  42.  
  43.  
  44.  
  45.         PhoneStateIntentReceiver pstr = null;
  46.  
  47.  
  48.  
  49.         class MyEventHandler implements EventHandler {
  50.  
  51.                 private ByteArrayOutputStream baos = new ByteArrayOutputStream();
  52.  
  53.                  private Activity activity;
  54.  
  55.             MyEventHandler(Activity activity) {
  56.  
  57.                 this.activity = activity;
  58.  
  59.             }
  60.  
  61.  
  62.  
  63.                 public void data(byte[] bytes, int len) {
  64.  
  65.                         baos.write(bytes, 0, len);
  66.  
  67.                 }
  68.  
  69.                 public void endData() {
  70.  
  71.                         String text = new String(baos.toByteArray());
  72.  
  73.                         Log.d("http", "load data [" + text + "]");
  74.  
  75.                 }
  76.  
  77.                 public void status(int arg0, int arg1, int arg2, String s) {
  78.  
  79.                         Log.d("http", "status [" + s + "]");
  80.  
  81.                 }
  82.  
  83.  
  84.  
  85.             public void error(int i, String s) {
  86.  
  87.                 Log.d("http", "error [" + s + "]");
  88.  
  89.                 NotificationManager nm = (NotificationManager)
  90.  
  91.                         activity.getSystemService(Activity.NOTIFICATION_SERVICE);
  92.  
  93.  
  94.  
  95.                 nm.notifyWithText(100,
  96.  
  97.                         "error [" + s + "]",
  98.  
  99.                         NotificationManager.LENGTH_SHORT, null);
  100.  
  101.             }
  102.  
  103.                 public void handleSslErrorRequest(int arg0, String arg1, SslCertificate arg2) { }
  104.  
  105.                 public void headers(Iterator arg0) { }
  106.  
  107.                 public void headers(Headers arg0) { }
  108.  
  109.         }
  110.  
  111.  
  112.  
  113.         @Override
  114.  
  115.         public void onCreate(Bundle icicle) {
  116.  
  117.                 super.onCreate(icicle);
  118.  
  119.  
  120.  
  121.                 InputStream in = this.getResources().openRawResource(R.raw.everlast);
  122.  
  123.                 System.out.println(in.toString());
  124.  
  125.  
  126.  
  127.                 android.net.http.RequestQueue rQueue = new RequestQueue(this);
  128.  
  129.                 String POSTText;
  130.  
  131.                 try {
  132.  
  133.                         POSTText = "data=" + URLEncoder.encode("HELLO ANDROID", "UTF-8");
  134.  
  135.                 } catch (UnsupportedEncodingException e) {
  136.  
  137.                         return;
  138.  
  139.                 }
  140.  
  141.                 byte[] POSTbytes = POSTText.getBytes();
  142.  
  143.                 ByteArrayInputStream baos = new ByteArrayInputStream(POSTbytes);
  144.  
  145.                 rQueue.queueRequest("http://www.anddev.org/postresponse.php", "POST", null,
  146.  
  147.                                 new MyEventHandler(this), baos, POSTbytes.length, false);
  148.  
  149.  
  150.  
  151.                 rQueue.waitUntilComplete();
  152.  
  153.         }
  154.  
  155. }
Parsed in 0.045 seconds, using GeSHi 1.0.8.4


What I see in the LogCat is the following:
Code: Select all
D/http(1342): load data [POSTed data: '' GETed data: '']


When I change the request to:
rQueue.queueRequest("http://www.anddev.org/postresponse.php?data=Hello", "POST", null,
new MyEventHandler(this), baos, POSTbytes.length, false);

I receive:
Code: Select all
D/http(1342): load data [POSTed data: '' GETed data: 'Hello']


But that is still no POST, just GET... :(
I'll have another look at it this evening.

The code I altered comes from :src: here. Perhaps you can get it work. Let us know :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby plusminus » Mon Dec 17, 2007 5:04 pm

Hello_back squid,

Did you try:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. org.apache.commons.httpclient.methods.PostMethod
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

... as it was used :src: here.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby hama » Fri Dec 28, 2007 9:43 am

Hi, all

> But that is still no POST, just GET... :(

I think that Content-Type is essential for your server.

Try the following;

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     Map<String,String> headers = new  HashMap<String,String>();
  2.  
  3.     headers.put("Content-Type", "application/x-www-form-urlencoded");
  4.  
  5.        ...
  6.  
  7.     rQueue.queueRequest("http://www.anddev.org/postresponse.php", "POST", headers,
  8.  
  9.                    new MyEventHandler(this), baos, POSTbytes.length, false);
Parsed in 0.036 seconds, using GeSHi 1.0.8.4



hama
hama
Freshman
Freshman
 
Posts: 2
Joined: Fri Dec 28, 2007 9:30 am

Postby plusminus » Fri Dec 28, 2007 2:35 pm

Big thx hama :!:,

it works now :!: Output:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. D/http(1677): status [OK]
  2.  
  3. D/http(1677): load data [POSTed data: 'HELLO ANDROID' GETed data: '']
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


The following code works for me ( I'll transform this into a tutorial when I'm back from holidays: ~ 3 January 08 ):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.testproject;
  2.  
  3.  
  4.  
  5. import java.io.ByteArrayInputStream;
  6.  
  7. import java.io.ByteArrayOutputStream;
  8.  
  9. import java.io.InputStream;
  10.  
  11. import java.io.UnsupportedEncodingException;
  12.  
  13. import java.net.URLEncoder;
  14.  
  15. import java.util.HashMap;
  16.  
  17. import java.util.Iterator;
  18.  
  19. import java.util.Map;
  20.  
  21.  
  22.  
  23. import android.app.Activity;
  24.  
  25. import android.app.ListActivity;
  26.  
  27. import android.app.NotificationManager;
  28.  
  29. import android.net.http.EventHandler;
  30.  
  31. import android.net.http.Headers;
  32.  
  33. import android.net.http.RequestQueue;
  34.  
  35. import android.net.http.SslCertificate;
  36.  
  37. import android.os.Bundle;
  38.  
  39. import android.util.Log;
  40.  
  41.  
  42.  
  43. public class TestLayout extends ListActivity {
  44.  
  45.  
  46.  
  47.      class MyEventHandler implements EventHandler {
  48.  
  49.           private ByteArrayOutputStream baos = new ByteArrayOutputStream();
  50.  
  51.            private Activity activity;
  52.  
  53.          MyEventHandler(Activity activity) {
  54.  
  55.              this.activity = activity;
  56.  
  57.          }
  58.  
  59.  
  60.  
  61.           public void data(byte[] bytes, int len) {
  62.  
  63.                baos.write(bytes, 0, len);
  64.  
  65.           }
  66.  
  67.           public void endData() {
  68.  
  69.                String text = new String(baos.toByteArray());
  70.  
  71.                Log.d("http", "load data [" + text + "]");
  72.  
  73.           }
  74.  
  75.           public void status(int arg0, int arg1, int arg2, String s) {
  76.  
  77.                Log.d("http", "status [" + s + "]");
  78.  
  79.           }
  80.  
  81.  
  82.  
  83.          public void error(int i, String s) {
  84.  
  85.              Log.d("http", "error [" + s + "]");
  86.  
  87.              NotificationManager nm = (NotificationManager)
  88.  
  89.                      activity.getSystemService(Activity.NOTIFICATION_SERVICE);
  90.  
  91.  
  92.  
  93.              nm.notifyWithText(0x1337,
  94.  
  95.                      "error [" + s + "]",
  96.  
  97.                      NotificationManager.LENGTH_SHORT, null);
  98.  
  99.          }
  100.  
  101.           public void handleSslErrorRequest(int arg0, String arg1, SslCertificate arg2) { }
  102.  
  103.           public void headers(Iterator arg0) { }
  104.  
  105.           public void headers(Headers arg0) { }
  106.  
  107.      }
  108.  
  109.  
  110.  
  111.      @Override
  112.  
  113.      public void onCreate(Bundle icicle) {
  114.  
  115.           super.onCreate(icicle);
  116.  
  117.  
  118.  
  119.           InputStream in = this.getResources().openRawResource(R.raw.everlast);
  120.  
  121.           System.out.println(in.toString());
  122.  
  123.  
  124.  
  125.           android.net.http.RequestQueue rQueue = new RequestQueue(this);
  126.  
  127.           String POSTText;
  128.  
  129.           try {
  130.  
  131.                POSTText = "data=" + URLEncoder.encode("HELLO ANDROID", "UTF-8");
  132.  
  133.           } catch (UnsupportedEncodingException e) {
  134.  
  135.                return;
  136.  
  137.           }
  138.  
  139.           byte[] POSTbytes = POSTText.getBytes();
  140.  
  141.           ByteArrayInputStream baos = new ByteArrayInputStream(POSTbytes);
  142.  
  143.          
  144.  
  145.           Map<String,String> headers = new  HashMap<String,String>();
  146.  
  147.           headers.put("Content-Type", "application/x-www-form-urlencoded");
  148.  
  149.          
  150.  
  151.           rQueue.queueRequest("http://www.anddev.org/postresponse.php", "POST", headers,
  152.  
  153.                     new MyEventHandler(this), baos, POSTbytes.length, false);
  154.  
  155.  
  156.  
  157.           rQueue.waitUntilComplete();
  158.  
  159.      }
  160.  
  161. }
Parsed in 0.051 seconds, using GeSHi 1.0.8.4


Best Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby plusminus » Thu Jan 10, 2008 10:28 pm

Wohoo I found some time somewhere in the corner of my room and transformed into an actual tutorial.

:arrow: :src: Watch it.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Re: HTTP Post, Web Services in Android

Postby libertino » Thu May 29, 2008 6:02 pm

httpclient from apache is well suted for this
libertino
Junior Developer
Junior Developer
 
Posts: 12
Joined: Sun May 18, 2008 3:36 pm

HTTP POST-Reply sample working on SDK 0.9

Postby Hans » Sun Oct 12, 2008 3:47 pm

Hi,
anyone has a recent HTTP POST-Reply sample working on SDK 0.9 available
please?
Any help is greatly appreciated.

Thanks
Hans
Freshman
Freshman
 
Posts: 3
Joined: Fri Oct 03, 2008 8:24 am

Postby cassianotartari » Tue Nov 04, 2008 6:37 pm

Somebody can convert this tutorial to SDK 1.0 ?
public class pessoa {
private String nome = "Cassiano Tartari";
private String url = "www.cassianotartari.eng.br"
}
User avatar
cassianotartari
Developer
Developer
 
Posts: 28
Joined: Thu Aug 28, 2008 3:42 pm

Postby shabeervga » Thu Nov 06, 2008 1:00 pm

import android.net.http.EventHandler;
import android.net.http.Headers;
import android.net.http.RequestQueue;

i am getting error like this " cannot be resolved " from Eclipse

please help me for solving this problem
shabeervga
Freshman
Freshman
 
Posts: 2
Joined: Thu Nov 06, 2008 12:51 pm
Location: Trivadurm

Postby jasont » Thu Nov 06, 2008 4:22 pm

shabeervga wrote:import android.net.http.EventHandler;
import android.net.http.Headers;
import android.net.http.RequestQueue;

i am getting error like this " cannot be resolved " from Eclipse

please help me for solving this problem


Thats because those packages are not available in the SDK.

http://code.google.com/android/referenc ... mmary.html
jasont
Developer
Developer
 
Posts: 28
Joined: Mon Sep 22, 2008 6:39 pm
Location: California

Postby Surbhit » Wed Nov 19, 2008 5:54 pm

import android.net.http.EventHandler;
import android.net.http.Headers;
import android.net.http.RequestQueue;

The above mentioned classes are no longer in android.

Can you tell me what to use instead of these classes or could you modify the code to work on current android sdk ?
Surbhit
Freshman
Freshman
 
Posts: 4
Joined: Mon Nov 17, 2008 4:24 pm

Postby cassianotartari » Wed Nov 19, 2008 6:46 pm

public class pessoa {
private String nome = "Cassiano Tartari";
private String url = "www.cassianotartari.eng.br"
}
User avatar
cassianotartari
Developer
Developer
 
Posts: 28
Joined: Thu Aug 28, 2008 3:42 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 1 guest