How to send data from a client to a server via the POST method.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lackerstef
    New Member
    • May 2010
    • 2

    #1

    How to send data from a client to a server via the POST method.

    I am trying to send a few NameValuePair pieces of data to a server. I wanted to know how I can turn my current GET request into a POST request so that the data encapsulation is not done so merely in a URL string. Here is what I am using now:
    Code:
    /**
    	 * Connect to the supplied URL using GET	 
    * @param urlString
    	 * @return true for we receive HTTP_OK from http call 
    	 */
    	
    	private boolean connectToURL(Location loc)
    	{
     
    		double latitude = loc.getLatitude();
    		double longitude = loc.getLongitude();
    		//double accuracy = loc.getAccuracy();
    		//double altitude = loc.getAltitude();
    		//double bearing = loc.getBearing();
    		
    		CoordinateConversion cc = new CoordinateConversion();
    		String utmconverted = cc.latLon2UTM(latitude, longitude);
    		
           String uArg="key="+URLEncoder.encode(key)+"&tag="+URLEncoder.encode(tag)+"&utm="+URLEncoder.encode(""+utmconverted)/*+"&bearing="+URLEncoder.encode(""+bearing)"&accuracy="+URLEncoder.encode(""+accuracy)+"&alt="+URLEncoder.encode(""+altitude)*/;
     //       String uArg="key="+URLEncoder.encode(key)+"&tag="+URLEncoder.encode(tag)+"&latitude="+URLEncoder.encode(""+latitude)+"&longitude="+URLEncoder.encode(""+longitude);
    
            String uString=url+uArg; //Assumes url ends with "/"
            Log.e("APP", "Output = "+uString);
    		boolean res=false;
    
    		try
    		{		
    
    			
    			
    			URL url=new URL(uString);
    			 
    			 
    			URLConnection uConnection = url.openConnection();
    
    			if (!(uConnection instanceof HttpURLConnection))                     
    				return false;
    			
    			HttpURLConnection connection = (HttpURLConnection) uConnection;
    			connection.setRequestMethod("GET");
    			connection.setInstanceFollowRedirects(true);
    			connection.setAllowUserInteraction(false);
    			connection.connect(); 
    
    			if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) 
    			{
    				res=true;
    			}	
    			connection.disconnect();
    		}
    		catch (Exception ex)
    		{
    			res=false; // If disconnect fails
    		}
    
    		return res;
    		
    			
    			
    		
    	}
    Last edited by Niheel; May 24 '10, 06:19 PM. Reason: code tags
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Can't you simply set:
    connection.setR equestMethod("P OST");
    ?

    Is there a specific problem you are having?

    Comment

    • lackerstef
      New Member
      • May 2010
      • 2

      #3
      Originally posted by jkmyoung
      Can't you simply set:
      connection.setR equestMethod("P OST");
      ?

      Is there a specific problem you are having?
      Will that not just send the URL again though? Thanks for the reply!

      Comment

      Working...