Http POST SDK 1.0

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

Http POST SDK 1.0

Postby cassianotartari » Fri Nov 07, 2008 2:00 pm

Hi,

For many: my solution for Http Post

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package gpsbusca.com.br;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.UnsupportedEncodingException;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.Iterator;
  11. import java.util.Map;
  12. import java.util.Set;
  13.  
  14. import org.apache.http.HttpEntity;
  15. import org.apache.http.HttpResponse;
  16. import org.apache.http.client.CookieStore;
  17. import org.apache.http.client.entity.UrlEncodedFormEntity;
  18. import org.apache.http.client.methods.HttpPost;
  19. import org.apache.http.impl.client.DefaultHttpClient;
  20. import org.apache.http.message.BasicNameValuePair;
  21.  
  22. import android.app.Activity;
  23. import android.os.Bundle;
  24. import android.util.Log;
  25.  
  26. public class HttpPostConnection extends Activity {
  27.        
  28.         private CookieStore cookies;
  29.         private boolean isAuthenticated;
  30.         private ArrayList<BasicNameValuePair> pairs;
  31.         private DefaultHttpClient httpclient;
  32.         private HttpPost httppost;
  33.         private InputStream conteudo;
  34.         private String retornoConexao;
  35.        
  36.     /** Called when the activity is first created. */
  37.     @Override
  38.     public void onCreate(Bundle savedInstanceState) {
  39.         super.onCreate(savedInstanceState);
  40.         setContentView(R.layout.main);
  41.  
  42.         //example of use
  43.         Map<String,String> vars = new HashMap<String,String>();
  44.         vars.put("login", "login");
  45.         vars.put("password", "password");
  46.         vars.put("action", "del");
  47.  
  48.         parametrosHttp("http://www.rgcarros.com.br", vars);
  49.         go();
  50.     }
  51.        
  52.         public void parametrosHttp(String url, Map<String,String> variables) {
  53.                 this.httpclient = new DefaultHttpClient();
  54.                 this.httppost = new HttpPost(url);
  55.                 this.pairs = new ArrayList();
  56.                 if(variables != null){
  57.                         Set keys = variables.keySet();
  58.                         for(Iterator i = keys.iterator(); i.hasNext();) {
  59.                                 String key = (String)i.next();
  60.                                 pairs.add(new BasicNameValuePair(key, variables.get(key)));
  61.                         }
  62.                 }
  63.         }
  64.        
  65.         public String go(){
  66.                 try {
  67.                     UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(pairs, "ISO-8859-1");
  68.                
  69.                     /** Assign the POST data to the entity */
  70.                     httppost.setEntity(p_entity);
  71.                
  72.                     /** Perform the actual HTTP POST */
  73.                     HttpResponse response = httpclient.execute(httppost);
  74.                     HttpEntity entity = response.getEntity();
  75.                     conteudo = entity.getContent();
  76.                     this.retornoConexao = convertStreamToString(conteudo);
  77.                     Log.d("HttpPostConnection",">>>>>>>>>>>>>>> "+retornoConexao);
  78.                     int status_code = response.getStatusLine().getStatusCode();
  79.                     this.cookies = httpclient.getCookieStore();
  80.                     if (status_code >= 300) {
  81.                         this.isAuthenticated = false;
  82.                     } else {
  83.                         this.isAuthenticated = true;
  84.                     }
  85.                 } catch (UnsupportedEncodingException uee) {
  86.                     // Woops
  87.                 } catch (IOException ioe) {
  88.                     // Woops
  89.                 } catch(IllegalStateException ise) {
  90.                         // woops
  91.                 }
  92.                 return retornoConexao;
  93.         }
  94.        
  95.         public String convertStreamToString(InputStream is) {
  96.                 BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  97.                 StringBuilder sb = new StringBuilder();
  98.  
  99.                 String line = null;
  100.                 try {
  101.                         while ((line = reader.readLine()) != null) {
  102.                                 sb.append(line + "n");
  103.                         }
  104.                 } catch (IOException e) {
  105.                         e.printStackTrace();
  106.                 } finally {
  107.                         try {
  108.                                 is.close();
  109.                         } catch (IOException e) {
  110.                                 e.printStackTrace();
  111.                         }
  112.                 }
  113.  
  114.                 return sb.toString();
  115.         }
  116.  
  117.         public boolean isAuthenticated() {
  118.                 return isAuthenticated;
  119.         }
  120. }
Parsed in 0.045 seconds, using GeSHi 1.0.8.4
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

Postby haden » Tue Dec 16, 2008 12:54 pm

Thanks buddy.. was looking for this..
haden
Experienced Developer
Experienced Developer
 
Posts: 74
Joined: Thu Sep 11, 2008 11:51 am

Postby wescorp » Thu Feb 05, 2009 6:37 pm

Thanks a lot for this. It is working well for me also.

I was generating a warning from this line
BufferedReader reader = new BufferedReader(new InputStreamReader(is));

and replaced it with

BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8*1024);

and the warning went away so all is well.
Cheers,
Wes
wescorp
Freshman
Freshman
 
Posts: 5
Joined: Thu Jan 03, 2008 8:23 pm
Location: Meridian, Idaho USA

Time-out

Postby northvortex » Sat Feb 07, 2009 4:14 am

I also added code to define a connection time-out.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. HttpParams params = new BasicHttpParams();                     
  2. params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
  3. params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
  4.  
  5. this.httppost.setParams(params);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


more information:

Interface CoreConnectionPNames
http://hc.apache.org/httpcomponents-cor ... Names.html
User avatar
northvortex
Freshman
Freshman
 
Posts: 4
Joined: Sun Dec 09, 2007 7:39 pm

Postby E-Nature » Sat Mar 06, 2010 8:20 pm

I have a question. is go() supposed to return what the PHP file is outputting?
I can send data via POST to the PHP file but the Java method does not receive back the return value from the PHP file (just an empty string)

thank you for elaboration.
User avatar
E-Nature
Experienced Developer
Experienced Developer
 
Posts: 93
Joined: Mon Nov 16, 2009 3:39 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 4 guests