send datas from Android to web-Sever

Tutorials with advanced 'difficulty' and more Lines of Code.

send datas from Android to web-Sever

Postby fbesoain » Sat May 09, 2009 4:55 pm

hello everyone,

I am trying to figure out how I can do that, send two points of latitude and longitude from Android at webserver or a website,
¿it's possible, is it? how I can do that?

thanks

Felipe
fbesoain
Freshman
Freshman
 
Posts: 6
Joined: Wed May 06, 2009 2:29 am
Location: Chile

Top

Postby for1nall » Thu Jun 11, 2009 8:05 am

U can send the data by adding the co-ordinates in the http url request and send it to the server......but the catch is the webserver should have a page which uses the input that u r sending and process it and send the response as u desire..
From,
Shashank
for1nall
Freshman
Freshman
 
Posts: 2
Joined: Thu Jun 11, 2009 5:53 am
Location: Hyderabad

Postby fbesoain » Thu Jun 11, 2009 7:23 pm

for1nall wrote:U can send the data by adding the co-ordinates in the http url request and send it to the server......but the catch is the webserver should have a page which uses the input that u r sending and process it and send the response as u desire..
From,
Shashank



thanks for replying me, but I still trying to figure out how I can send the co-ordinates in a http url request... do you have any idea?
fbesoain
Freshman
Freshman
 
Posts: 6
Joined: Wed May 06, 2009 2:29 am
Location: Chile

Postby shiteiru » Fri Jun 12, 2009 12:32 pm

Basically, if you don't know how to deal with a http post, and to pass on variables with simple URL handling, it will be hard for you to get what you want ^^
Nevertheless........i will provide you a code sample, that's working with 1.5.

What this will do (simply said): establish a connection to a certain website. Pass on some variables per httppost that you specified. Finally it will retrieve and temporarily save cookies needed to authenticate with the server and to stay logged in.

I wrote this Class to login and handle some http post requests for this site: myanimelist.net

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package de.mal;
  3.  
  4.  
  5.  
  6. import java.io.BufferedReader;
  7.  
  8. import java.io.DataInputStream;
  9.  
  10. import java.io.FileInputStream;
  11.  
  12. import java.io.IOException;
  13.  
  14. import java.io.InputStream;
  15.  
  16. import java.io.InputStreamReader;
  17.  
  18. import java.io.UnsupportedEncodingException;
  19.  
  20. import java.util.ArrayList;
  21.  
  22. import java.util.HashMap;
  23.  
  24. import java.util.Iterator;
  25.  
  26. import java.util.List;
  27.  
  28. import java.util.Map;
  29.  
  30. import java.util.Set;
  31.  
  32.  
  33.  
  34. import org.apache.http.Header;
  35.  
  36. import org.apache.http.HttpEntity;
  37.  
  38. import org.apache.http.HttpResponse;
  39.  
  40. import org.apache.http.NameValuePair;
  41.  
  42. import org.apache.http.client.CookieStore;
  43.  
  44. import org.apache.http.client.entity.UrlEncodedFormEntity;
  45.  
  46. import org.apache.http.client.methods.HttpPost;
  47.  
  48. import org.apache.http.impl.client.DefaultHttpClient;
  49.  
  50. import org.apache.http.message.BasicHeader;
  51.  
  52. import org.apache.http.message.BasicNameValuePair;
  53.  
  54. import org.apache.http.protocol.HTTP;
  55.  
  56.  
  57.  
  58. import android.app.Activity;
  59.  
  60. import android.app.ProgressDialog;
  61.  
  62. import android.database.Cursor;
  63.  
  64. import android.os.Bundle;
  65.  
  66. import android.util.Log;
  67.  
  68. import de.mal.Db.DbAdapter;
  69.  
  70.  
  71.  
  72. public class Update3 extends Activity {
  73.  
  74.      
  75.  
  76.          private DbAdapter mDbHelper;
  77.  
  78.      private CookieStore cookies;
  79.  
  80.      private boolean isAuthenticated;
  81.  
  82.      private ArrayList<BasicNameValuePair> pairs;
  83.  
  84.      private DefaultHttpClient httpclient;
  85.  
  86.      private HttpPost httppost, httppost2;
  87.  
  88.      private InputStream content, content2;
  89.  
  90.      private String user, pass, returnStream, returnStream2;
  91.  
  92.      ProgressDialog myProgressDialog = null;
  93.  
  94.      
  95.  
  96.     /** Called when the activity is first created. */
  97.  
  98.     @Override
  99.  
  100.     public void onCreate(Bundle savedInstanceState) {
  101.  
  102.         super.onCreate(savedInstanceState);
  103.  
  104.        
  105.  
  106.        
  107.  
  108.                  myProgressDialog = ProgressDialog.show(Update3.this,    
  109.  
  110.                            "Please wait...", "Exporting Changes...", true);
  111.  
  112.                  
  113.  
  114.                  new Thread() {
  115.  
  116.                       public void run() {
  117.  
  118.                            try{
  119.  
  120.                                 // Do some Fake-Work
  121.  
  122.                                 sleep(5000);
  123.  
  124.                            } catch (Exception e) {  }
  125.  
  126.                            // Dismiss the Dialog
  127.  
  128.                            myProgressDialog.dismiss();
  129.  
  130.                       }
  131.  
  132.                  }.start();
  133.  
  134.            
  135.  
  136.    
  137.  
  138.        
  139.  
  140.         mDbHelper = new DbAdapter(this);
  141.  
  142.         mDbHelper.open();
  143.  
  144.        
  145.  
  146.         setContentView(R.layout.main);
  147.  
  148.        
  149.  
  150.         try{
  151.  
  152.                  FileInputStream fIn = openFileInput("user.txt");      
  153.  
  154.                 DataInputStream in = new DataInputStream(fIn);
  155.  
  156.              BufferedReader br = new BufferedReader(new InputStreamReader(in));
  157.  
  158.                 // Read file with UTF-8
  159.  
  160.              user = br.readLine();  
  161.  
  162.              
  163.  
  164.              FileInputStream fIn2 = openFileInput("pass.txt");      
  165.  
  166.                 DataInputStream in2 = new DataInputStream(fIn2);
  167.  
  168.              BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
  169.  
  170.                 pass = br2.readLine();
  171.  
  172.         }
  173.  
  174.         catch (IOException ioe) {
  175.  
  176.             ioe.printStackTrace();
  177.  
  178.         }
  179.  
  180.        
  181.  
  182.         Map<String,String> vars = new HashMap<String,String>();
  183.  
  184.         vars.put("username", user);
  185.  
  186.         vars.put("password", pass);
  187.  
  188.  
  189.  
  190.         parametersHttp("http://myanimelist.net/login.php", vars);
  191.  
  192.         go();
  193.  
  194.        
  195.  
  196.        
  197.  
  198.     }
  199.  
  200.    
  201.  
  202.      public void parametersHttp(String url, Map<String,String> variables) {
  203.  
  204.           this.httpclient = new DefaultHttpClient();
  205.  
  206.           this.httppost = new HttpPost(url);
  207.  
  208.           this.pairs = new ArrayList();
  209.  
  210.           if(variables != null){
  211.  
  212.                Set keys = variables.keySet();
  213.  
  214.                for(Iterator i = keys.iterator(); i.hasNext();) {
  215.  
  216.                     String key = (String)i.next();
  217.  
  218.                     pairs.add(new BasicNameValuePair(key, variables.get(key)));
  219.  
  220.                }
  221.  
  222.           }
  223.  
  224.      }
  225.  
  226.      
  227.  
  228.      public String go(){
  229.  
  230.           try {
  231.  
  232.                  
  233.  
  234.                   Cursor all_anime = mDbHelper.fetchAllUpdatedEntries();
  235.  
  236.                   startManagingCursor(all_anime);
  237.  
  238.                   //String[] row_id = new  String[]{mDbHelper.KEY_ROWID};
  239.  
  240.                   all_anime.moveToFirst();
  241.  
  242.                  
  243.  
  244.                   int id_Column = all_anime.getColumnIndex("_id");
  245.  
  246.                   int my_id_Column = all_anime.getColumnIndex("my_id");
  247.  
  248.                   int series_animedb_id_Column = all_anime.getColumnIndex("series_animedb_id");
  249.  
  250.                   int series_title_Column = all_anime.getColumnIndex("series_title");
  251.  
  252.                   int series_episodes_Column = all_anime.getColumnIndex("series_episodes");
  253.  
  254.                   int series_status_Column = all_anime.getColumnIndex("series_status");
  255.  
  256.                   int my_status_Column = all_anime.getColumnIndex("my_status");
  257.  
  258.                   int my_watched_episodes_Column = all_anime.getColumnIndex("my_watched_episodes");
  259.  
  260.                   int my_score_Column = all_anime.getColumnIndex("my_score");
  261.  
  262.                  
  263.  
  264.                   if(all_anime.isFirst()){
  265.  
  266.                           do{
  267.  
  268.                                   String id_Column_String = all_anime.getString(id_Column);
  269.  
  270.                                   String my_id_Column_String = all_anime.getString(my_id_Column);
  271.  
  272.                                   String series_animedb_id_Column_String = all_anime.getString(series_animedb_id_Column);
  273.  
  274.                                   String series_episodes_Column_String = all_anime.getString(series_episodes_Column);
  275.  
  276.                                   String series_status_Column_String = all_anime.getString(series_status_Column);
  277.  
  278.                                   String series_title_String = all_anime.getString(series_title_Column);
  279.  
  280.                                   String my_status_Column_String = all_anime.getString(my_status_Column);
  281.  
  282.                                   String my_watched_episodes_Column_String = all_anime.getString(my_watched_episodes_Column);
  283.  
  284.                                   String my_score_Column_String = all_anime.getString(my_score_Column);
  285.  
  286.                                                                  
  287.  
  288.                               UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(pairs, "ISO-8859-1");
  289.  
  290.                          
  291.  
  292.                               /** Assign the POST data to the entity */
  293.  
  294.                               httppost.setEntity(p_entity);
  295.  
  296.                               /** Perform the actual HTTP POST */
  297.  
  298.                              
  299.  
  300.                               HttpResponse response = httpclient.execute(httppost);
  301.  
  302.                               HttpEntity entity = response.getEntity();
  303.  
  304.                               content = entity.getContent();
  305.  
  306.                               this.returnStream = convertStreamToString(content);
  307.  
  308.                               int status_code = response.getStatusLine().getStatusCode();
  309.  
  310.                               this.cookies = httpclient.getCookieStore();
  311.  
  312.                              
  313.  
  314.                               Log.d("httpPost", "Post logon cookies:" + cookies.toString());
  315.  
  316.                               if (status_code >= 300) {
  317.  
  318.                                   this.isAuthenticated = false;
  319.  
  320.                               } else {
  321.  
  322.                                   this.isAuthenticated = true;
  323.  
  324.                                   List <NameValuePair> nvps = new ArrayList <NameValuePair>();
  325.  
  326.                                   nvps.add(new BasicNameValuePair("series_id", my_id_Column_String)); //6422692
  327.  
  328.                                   nvps.add(new BasicNameValuePair("anime_db_series_id", series_animedb_id_Column_String));
  329.  
  330.                                   nvps.add(new BasicNameValuePair("aeps", series_episodes_Column_String));
  331.  
  332.                                   nvps.add(new BasicNameValuePair("astatus", series_status_Column_String));
  333.  
  334.                                   nvps.add(new BasicNameValuePair("close_on_update", "true"));
  335.  
  336.                                   nvps.add(new BasicNameValuePair("series_title", series_animedb_id_Column_String));
  337.  
  338.                                   nvps.add(new BasicNameValuePair("status", my_status_Column_String));
  339.  
  340.                                   nvps.add(new BasicNameValuePair("completed_eps", my_watched_episodes_Column_String));
  341.  
  342.                                   nvps.add(new BasicNameValuePair("score", my_score_Column_String));
  343.  
  344.                                   nvps.add(new BasicNameValuePair("submitIt", "2"));
  345.  
  346.                
  347.  
  348.                                   httpclient.getCookieStore();
  349.  
  350.                                   this.httppost2 = new HttpPost("http://myanimelist.net/panel.php?keepThis=true&go=edit&id=4541138&hidenav=true");
  351.  
  352.                                   httppost2.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
  353.  
  354.                                  
  355.  
  356.                                   Header[] headers = new BasicHeader[2];
  357.  
  358.                                   headers[0] = new BasicHeader("Accept","application/xml");
  359.  
  360.                                   headers[1] = new BasicHeader("Content-type", "application/x-www-form-urlencoded");
  361.  
  362.                                   httppost2.addHeader(headers[0]);
  363.  
  364.                                   httppost2.addHeader(headers[1]);
  365.  
  366.                                  
  367.  
  368.                                   HttpResponse response2 = httpclient.execute(httppost2);
  369.  
  370.                                   HttpEntity entity2 = response2.getEntity();
  371.  
  372.                                   content2 = entity2.getContent();
  373.  
  374.                                   this.returnStream2 = convertStreamToString(content2);
  375.  
  376.                                  
  377.  
  378.                                   int rowId = Integer.parseInt(id_Column_String);
  379.  
  380.                                   mDbHelper.setUpdated(rowId);
  381.  
  382.                               }
  383.  
  384.                           }while(all_anime.moveToNext());
  385.  
  386.               }
  387.  
  388.           } catch (UnsupportedEncodingException uee) {
  389.  
  390.               // Woops
  391.  
  392.           } catch (IOException ioe) {
  393.  
  394.               // Woops
  395.  
  396.           } catch(IllegalStateException ise) {
  397.  
  398.                // woops
  399.  
  400.           }
  401.  
  402.           return returnStream2;
  403.  
  404.  
  405.  
  406.      }
  407.  
  408.      
  409.  
  410.        
  411.  
  412.      
  413.  
  414.      public String convertStreamToString(InputStream is) {
  415.  
  416.           BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  417.  
  418.           StringBuilder sb = new StringBuilder();
  419.  
  420.  
  421.  
  422.           String line = null;
  423.  
  424.           try {
  425.  
  426.                while ((line = reader.readLine()) != null) {
  427.  
  428.                     sb.append(line + "\n");
  429.  
  430.                }
  431.  
  432.           } catch (IOException e) {
  433.  
  434.                e.printStackTrace();
  435.  
  436.           } finally {
  437.  
  438.                try {
  439.  
  440.                     is.close();
  441.  
  442.                } catch (IOException e) {
  443.  
  444.                     e.printStackTrace();
  445.  
  446.                }
  447.  
  448.           }
  449.  
  450.  
  451.  
  452.           return sb.toString();
  453.  
  454.      }
  455.  
  456.  
  457.  
  458.      public boolean isAuthenticated() {
  459.  
  460.           return isAuthenticated;
  461.  
  462.      }
  463.  
  464. }
  465.  
  466.  
Parsed in 0.073 seconds, using GeSHi 1.0.8.4
shiteiru
Junior Developer
Junior Developer
 
Posts: 23
Joined: Tue May 20, 2008 7:16 pm

hi

Postby Santhosh Ejanthkar » Tue Oct 06, 2009 2:43 pm

Hi

i ve seen u r code to send data from emulator to server. can u send in detail because i ve taken but one DbAdapter class is missing in it. actually it this article is posted on 20 may2009.

Acutally i want to send data from emulator to server and vice-versa.

please find it and send
Santhosh Ejathkar
santhosh.1984e@gmail.com
Santhosh Ejanthkar
Freshman
Freshman
 
Posts: 2
Joined: Sat Sep 12, 2009 8:15 am

Postby David Shaw » Fri Mar 26, 2010 1:54 pm

Hi

I'm new to this so hope this is not a stupid question,
But I wonted to use this code but can't as I don't have the class "de.mal.Db.DbAdapter"
Where would I go about getting it or how would I replace it with a different class?

Thanks for any help.
David Shaw
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Mar 26, 2010 1:07 pm

Top

server side code

Postby dj_4war » Mon May 10, 2010 7:03 am

shiteiru wrote:Basically, if you don't know how to deal with a http post, and to pass on variables with simple URL handling, it will be hard for you to get what you want ^^
Nevertheless........i will provide you a code sample, that's working with 1.5.

What this will do (simply said): establish a connection to a certain website. Pass on some variables per httppost that you specified. Finally it will retrieve and temporarily save cookies needed to authenticate with the server and to stay logged in.

I wrote this Class to login and handle some http post requests for this site: myanimelist.net

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package de.mal;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.DataInputStream;
  5. import java.io.FileInputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.io.UnsupportedEncodingException;
  10. import java.util.ArrayList;
  11. import java.util.HashMap;
  12. import java.util.Iterator;
  13. import java.util.List;
  14. import java.util.Map;
  15. import java.util.Set;
  16.  
  17. import org.apache.http.Header;
  18. import org.apache.http.HttpEntity;
  19. import org.apache.http.HttpResponse;
  20. import org.apache.http.NameValuePair;
  21. import org.apache.http.client.CookieStore;
  22. import org.apache.http.client.entity.UrlEncodedFormEntity;
  23. import org.apache.http.client.methods.HttpPost;
  24. import org.apache.http.impl.client.DefaultHttpClient;
  25. import org.apache.http.message.BasicHeader;
  26. import org.apache.http.message.BasicNameValuePair;
  27. import org.apache.http.protocol.HTTP;
  28.  
  29. import android.app.Activity;
  30. import android.app.ProgressDialog;
  31. import android.database.Cursor;
  32. import android.os.Bundle;
  33. import android.util.Log;
  34. import de.mal.Db.DbAdapter;
  35.  
  36. public class Update3 extends Activity {
  37.      
  38.          private DbAdapter mDbHelper;
  39.      private CookieStore cookies;
  40.      private boolean isAuthenticated;
  41.      private ArrayList<BasicNameValuePair> pairs;
  42.      private DefaultHttpClient httpclient;
  43.      private HttpPost httppost, httppost2;
  44.      private InputStream content, content2;
  45.      private String user, pass, returnStream, returnStream2;
  46.      ProgressDialog myProgressDialog = null;
  47.      
  48.     /** Called when the activity is first created. */
  49.     @Override
  50.     public void onCreate(Bundle savedInstanceState) {
  51.         super.onCreate(savedInstanceState);
  52.        
  53.        
  54.                  myProgressDialog = ProgressDialog.show(Update3.this,    
  55.                            "Please wait...", "Exporting Changes...", true);
  56.                  
  57.                  new Thread() {
  58.                       public void run() {
  59.                            try{
  60.                                 // Do some Fake-Work
  61.                                 sleep(5000);
  62.                            } catch (Exception e) {  }
  63.                            // Dismiss the Dialog
  64.                            myProgressDialog.dismiss();
  65.                       }
  66.                  }.start();
  67.            
  68.    
  69.        
  70.         mDbHelper = new DbAdapter(this);
  71.         mDbHelper.open();
  72.        
  73.         setContentView(R.layout.main);
  74.        
  75.         try{
  76.                  FileInputStream fIn = openFileInput("user.txt");      
  77.                 DataInputStream in = new DataInputStream(fIn);
  78.              BufferedReader br = new BufferedReader(new InputStreamReader(in));
  79.                 // Read file with UTF-8
  80.              user = br.readLine();  
  81.              
  82.              FileInputStream fIn2 = openFileInput("pass.txt");      
  83.                 DataInputStream in2 = new DataInputStream(fIn2);
  84.              BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
  85.                 pass = br2.readLine();
  86.         }
  87.         catch (IOException ioe) {
  88.             ioe.printStackTrace();
  89.         }
  90.        
  91.         Map<String,String> vars = new HashMap<String,String>();
  92.         vars.put("username", user);
  93.         vars.put("password", pass);
  94.  
  95.         parametersHttp("http://myanimelist.net/login.php", vars);
  96.         go();
  97.        
  98.        
  99.     }
  100.    
  101.      public void parametersHttp(String url, Map<String,String> variables) {
  102.           this.httpclient = new DefaultHttpClient();
  103.           this.httppost = new HttpPost(url);
  104.           this.pairs = new ArrayList();
  105.           if(variables != null){
  106.                Set keys = variables.keySet();
  107.                for(Iterator i = keys.iterator(); i.hasNext();) {
  108.                     String key = (String)i.next();
  109.                     pairs.add(new BasicNameValuePair(key, variables.get(key)));
  110.                }
  111.           }
  112.      }
  113.      
  114.      public String go(){
  115.           try {
  116.                  
  117.                   Cursor all_anime = mDbHelper.fetchAllUpdatedEntries();
  118.                   startManagingCursor(all_anime);
  119.                   //String[] row_id = new  String[]{mDbHelper.KEY_ROWID};
  120.                   all_anime.moveToFirst();
  121.                  
  122.                   int id_Column = all_anime.getColumnIndex("_id");
  123.                   int my_id_Column = all_anime.getColumnIndex("my_id");
  124.                   int series_animedb_id_Column = all_anime.getColumnIndex("series_animedb_id");
  125.                   int series_title_Column = all_anime.getColumnIndex("series_title");
  126.                   int series_episodes_Column = all_anime.getColumnIndex("series_episodes");
  127.                   int series_status_Column = all_anime.getColumnIndex("series_status");
  128.                   int my_status_Column = all_anime.getColumnIndex("my_status");
  129.                   int my_watched_episodes_Column = all_anime.getColumnIndex("my_watched_episodes");
  130.                   int my_score_Column = all_anime.getColumnIndex("my_score");
  131.                  
  132.                   if(all_anime.isFirst()){
  133.                           do{
  134.                                   String id_Column_String = all_anime.getString(id_Column);
  135.                                   String my_id_Column_String = all_anime.getString(my_id_Column);
  136.                                   String series_animedb_id_Column_String = all_anime.getString(series_animedb_id_Column);
  137.                                   String series_episodes_Column_String = all_anime.getString(series_episodes_Column);
  138.                                   String series_status_Column_String = all_anime.getString(series_status_Column);
  139.                                   String series_title_String = all_anime.getString(series_title_Column);
  140.                                   String my_status_Column_String = all_anime.getString(my_status_Column);
  141.                                   String my_watched_episodes_Column_String = all_anime.getString(my_watched_episodes_Column);
  142.                                   String my_score_Column_String = all_anime.getString(my_score_Column);
  143.                                                                  
  144.                               UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(pairs, "ISO-8859-1");
  145.                          
  146.                               /** Assign the POST data to the entity */
  147.                               httppost.setEntity(p_entity);
  148.                               /** Perform the actual HTTP POST */
  149.                              
  150.                               HttpResponse response = httpclient.execute(httppost);
  151.                               HttpEntity entity = response.getEntity();
  152.                               content = entity.getContent();
  153.                               this.returnStream = convertStreamToString(content);
  154.                               int status_code = response.getStatusLine().getStatusCode();
  155.                               this.cookies = httpclient.getCookieStore();
  156.                              
  157.                               Log.d("httpPost", "Post logon cookies:" + cookies.toString());
  158.                               if (status_code >= 300) {
  159.                                   this.isAuthenticated = false;
  160.                               } else {
  161.                                   this.isAuthenticated = true;
  162.                                   List <NameValuePair> nvps = new ArrayList <NameValuePair>();
  163.                                   nvps.add(new BasicNameValuePair("series_id", my_id_Column_String)); //6422692
  164.                                   nvps.add(new BasicNameValuePair("anime_db_series_id", series_animedb_id_Column_String));
  165.                                   nvps.add(new BasicNameValuePair("aeps", series_episodes_Column_String));
  166.                                   nvps.add(new BasicNameValuePair("astatus", series_status_Column_String));
  167.                                   nvps.add(new BasicNameValuePair("close_on_update", "true"));
  168.                                   nvps.add(new BasicNameValuePair("series_title", series_animedb_id_Column_String));
  169.                                   nvps.add(new BasicNameValuePair("status", my_status_Column_String));
  170.                                   nvps.add(new BasicNameValuePair("completed_eps", my_watched_episodes_Column_String));
  171.                                   nvps.add(new BasicNameValuePair("score", my_score_Column_String));
  172.                                   nvps.add(new BasicNameValuePair("submitIt", "2"));
  173.                
  174.                                   httpclient.getCookieStore();
  175.                                   this.httppost2 = new HttpPost("http://myanimelist.net/panel.php?keepThis=true&go=edit&id=4541138&hidenav=true");
  176.                                   httppost2.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
  177.                                  
  178.                                   Header[] headers = new BasicHeader[2];
  179.                                   headers[0] = new BasicHeader("Accept","application/xml");
  180.                                   headers[1] = new BasicHeader("Content-type", "application/x-www-form-urlencoded");
  181.                                   httppost2.addHeader(headers[0]);
  182.                                   httppost2.addHeader(headers[1]);
  183.                                  
  184.                                   HttpResponse response2 = httpclient.execute(httppost2);
  185.                                   HttpEntity entity2 = response2.getEntity();
  186.                                   content2 = entity2.getContent();
  187.                                   this.returnStream2 = convertStreamToString(content2);
  188.                                  
  189.                                   int rowId = Integer.parseInt(id_Column_String);
  190.                                   mDbHelper.setUpdated(rowId);
  191.                               }
  192.                           }while(all_anime.moveToNext());
  193.               }
  194.           } catch (UnsupportedEncodingException uee) {
  195.               // Woops
  196.           } catch (IOException ioe) {
  197.               // Woops
  198.           } catch(IllegalStateException ise) {
  199.                // woops
  200.           }
  201.           return returnStream2;
  202.  
  203.      }
  204.      
  205.        
  206.      
  207.      public String convertStreamToString(InputStream is) {
  208.           BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  209.           StringBuilder sb = new StringBuilder();
  210.  
  211.           String line = null;
  212.           try {
  213.                while ((line = reader.readLine()) != null) {
  214.                     sb.append(line + "n");
  215.                }
  216.           } catch (IOException e) {
  217.                e.printStackTrace();
  218.           } finally {
  219.                try {
  220.                     is.close();
  221.                } catch (IOException e) {
  222.                     e.printStackTrace();
  223.                }
  224.           }
  225.  
  226.           return sb.toString();
  227.      }
  228.  
  229.      public boolean isAuthenticated() {
  230.           return isAuthenticated;
  231.      }
  232. }
  233.  
Parsed in 0.073 seconds, using GeSHi 1.0.8.4


Thanks for the code,But could u pls provide server side code also...like how to get these parameters @ server end...if possible pls provide servelt code...@ server end
dj_4war
Junior Developer
Junior Developer
 
Posts: 10
Joined: Thu Apr 01, 2010 7:49 am

Re: send datas from Android to web-Sever

Postby ysubash » Wed Jun 02, 2010 2:00 pm

hai ..
iam working with android..
send me the code which connect the local host (server ) wit h android ...
its urgent...
ysubash
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Jun 01, 2010 6:12 am

Re: send datas from Android to web-Sever

Postby nikil » Fri Jun 11, 2010 8:35 am

hi i am giving some sample code to send some data to webserver

i.e upload file to the webserver


public void doSend(){
boolean success;
DefaultHttpClient httpclient = new DefaultHttpClient();

try {
File f = new File(myfilepath);

HttpPost httpost = new HttpPost(YOUR_SERVER_ADDRESS);
MultipartEntity entity = new MultipartEntity();
entity.addPart("justforchecking", new StringBody("Hello World"));
entity.addPart("userfile", new FileBody(f));
httpost.setEntity(entity);
HttpResponse response;
response = httpclient.execute(httpost);
Log.d("httpPost", "Login form get: " + response.getStatusLine());
Log.e(TAG,"Response Status linecode:"+response.getStatusLine());

if (entity != null) {
entity.consumeContent();
}
success = true;
} catch (Exception ex) {
Log.d("FormReviewer", "Upload failed: " + ex.getMessage() + " Stacktrace: " + ex.getStackTrace());
Log.e(TAG,"Upload failed: " + ex.getMessage() + " Stacktrace: " + ex.getStackTrace());
ex.printStackTrace();
success = false;
} finally {
httpclient.getConnectionManager().shutdown();

}
}


add this in an activity this code will upload a file to web server

myfilepath is path of file that u need to upload to web server
YOUR_SERVER_ADDRESS: ur server ip address


but u need add some external jars to the project like apache-mime4j-0.6.jar
commons-logging-1.1.1.jar
httpclient-4.0.jar
httpcore-4.0.1.jar
httpcore-nio-4.0.1.jar
httpmime-4.0.jar

download jar files from web and execute code
nikil
Freshman
Freshman
 
Posts: 2
Joined: Tue Dec 15, 2009 1:07 pm
Location: chennai

Re: send datas from Android to web-Sever

Postby Bourados » Mon Jul 26, 2010 7:36 pm

Hallo shiteiru,
you mentioned in your script "import de.mal.Db.DbAdapter"
What does this DdAdapter contain?

Thank you!!
User avatar
Bourados
Freshman
Freshman
 
Posts: 3
Joined: Mon Jul 26, 2010 5:22 pm

Re: send datas from Android to web-Sever

Postby torryton » Mon Nov 14, 2011 3:21 pm

I m implementing app on android platform with web services. I able to fetch data from server through api. Now I want to send data to server through api. I didn't got proper code. Please let me know the standard code for this or any example for it.
User avatar
torryton
Freshman
Freshman
 
Posts: 6
Joined: Wed Nov 09, 2011 3:14 pm

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests