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;
}
Comment