[Help] - Send HTTP POST

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

[Help] - Send HTTP POST

Postby Rui Goncalves » Fri Oct 23, 2009 4:07 pm

Hi there!

I am using the following program in order to test the send of HTTP POST message to a remote server:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package mobile.YourVideosMobile;
  3.  
  4.  
  5.  
  6. import java.io.IOException;
  7.  
  8. import java.net.Inet4Address;
  9.  
  10. import java.net.InetAddress;
  11.  
  12. import java.net.UnknownHostException;
  13.  
  14. import java.util.ArrayList;
  15.  
  16. import java.util.List;
  17.  
  18.  
  19.  
  20. import org.apache.http.HttpResponse;
  21.  
  22. import org.apache.http.NameValuePair;
  23.  
  24. import org.apache.http.client.ClientProtocolException;
  25.  
  26. import org.apache.http.client.HttpClient;
  27.  
  28. import org.apache.http.client.entity.UrlEncodedFormEntity;
  29.  
  30. import org.apache.http.client.methods.HttpPost;
  31.  
  32. import org.apache.http.impl.client.DefaultHttpClient;
  33.  
  34. import org.apache.http.message.BasicNameValuePair;
  35.  
  36.  
  37.  
  38. import android.app.Activity;
  39.  
  40. import android.os.Bundle;
  41.  
  42. import android.util.Log;
  43.  
  44. import android.widget.TextView;
  45.  
  46.  
  47.  
  48. public class YourVideos extends Activity {
  49.  
  50.         private static final String TAG = "YourVideos_DEBUG";
  51.  
  52.        
  53.  
  54.     /** Called when the activity is first created. */
  55.  
  56.     @Override
  57.  
  58.     public void onCreate(Bundle savedInstanceState) {
  59.  
  60.         super.onCreate(savedInstanceState);
  61.  
  62.        
  63.  
  64.         postData();    
  65.  
  66.     }
  67.  
  68.    
  69.  
  70.     public void postData() {
  71.  
  72.         // Create a new HttpClient and Post Header
  73.  
  74.         HttpClient httpclient = new DefaultHttpClient();
  75.  
  76.         HttpPost httppost = new HttpPost("http://yourvideos.servehttp.com:2323");
  77.  
  78.  
  79.  
  80.         try {
  81.  
  82.             // Add your data
  83.  
  84.             List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
  85.  
  86.             nameValuePairs.add(new BasicNameValuePair("id", "12345"));
  87.  
  88.             nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
  89.  
  90.             httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  91.  
  92.  
  93.  
  94.             // Execute HTTP Post Request
  95.  
  96.             HttpResponse response = httpclient.execute(httppost);
  97.  
  98.            
  99.  
  100.             Log.i(TAG, response.toString());
  101.  
  102.            
  103.  
  104.         } catch (ClientProtocolException e) {
  105.  
  106.                 Log.e(TAG, "Error in client protocol");
  107.  
  108.         } catch (IOException e) {
  109.  
  110.                 Log.e(TAG, "IOException");
  111.  
  112.         }
  113.  
  114.     }
  115.  
  116. }
  117.  
  118.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


but nothing is written in the logger. I've alreadt tried to establish a socket with server but it is not created; any possible problem with the port?

Thanks in advance,
Best regards!
Rui Goncalves
Freshman
Freshman
 
Posts: 6
Joined: Thu Jun 11, 2009 8:09 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 6 guests