Reputation: 31
I need to do a HTTP post to a web service..
If I place this into a web browser like this
http://server/ilwebservice.asmx/PlaceGPSCords?userid=99&longitude=-25.258&latitude=25.2548
then is stores the values to our DB on the server..
In Eclipse, using Java programming for android.. The url will look like
http://server/ilwebservice.asmx/PlaceGPSCords?userid="+uid+"&longitude="+lng1+"&latitude="+lat1+"
with uid
, lng1
and lat1
being assigned as strings..
How would I run this?
Thanks
Upvotes: 0
Views: 9609
Reputation: 67286
Infact I had done already what you are going to do. So try this stff. You can create a method like this what I have and pass your URL with lat, long. This is return you the response of the HTTP connection.
public static int sendData(String url) throws IOException
{
try{
urlobj = new URL(url);
conn = urlobj.openConnection();
httpconn= (HttpURLConnection)conn;
httpconn.setConnectTimeout(5000);
httpconn.setDoInput(true);
}
catch(Exception e){
e.printStackTrace();}
try{
responseCode = httpconn.getResponseCode();}
catch(Exception e){
responseCode = -1;
e.printStackTrace();
}
return responseCode;
}
Upvotes: 2
Reputation: 97
I'm not sure I understood your question, but if I did I think you can use java.net.UrlConnection :
URL url = new URL("http://server/ilwebservice.asmx/PlaceGPSCords?userid="+uid+"&longitude="+lng1+"&latitude="+lat1);
URLConnection conn = url.openConnection();
conn.connect();
Upvotes: 0
Reputation: 6335
For http post use name value pair. See below code -
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("longitude", long1));
nameValuePairs.add(new BasicNameValuePair("latitude", lat1));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
HttpResponse response = httpclient.execute(httppost);
Upvotes: 2
Reputation: 2216
try {
HttpClient client = new DefaultHttpClient();
String getURL = "http://server/ilwebservice.asmx/PlaceGPSCords?userid="+uid+"&longitude="+lng1+"&latitude="+lat1+";
HttpGet get = new HttpGet(getURL);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
//do something with the response
Log.i("GET RESPONSE",EntityUtils.toString(resEntityGet));
}
} catch (Exception e) {
e.printStackTrace();
}
Upvotes: 5