简体   繁体   中英

Passing parameters to web service method in c#

I'm developing an android application .. which will send location data to a web service to store in the server database.

In Java: I've used this protocol so the URI is: HTTP request instead of REST

HttpPost request = new HttpPost("http://trafficmapsa.com/GService.asmx/GPSdata?lon="+Lon+"&Lat="+Lat+"&speed="+speed);    

In Asp.net (c#) web service will be:

[WebMethod]
public CountryName GPSdata(Double Lon, Double Lat, Double speed)
{
    String ConnStr = ConfigurationManager.ConnectionStrings["TrafficMapConnectionString"].ConnectionString;
    SqlConnection myConnection = new SqlConnection(ConnStr);
    SqlCommand myCommand = new SqlCommand("INSERT INTO Traffic(Longitude,Latitude,Speed,Distance,Time,MAC_Address) VALUES('" + (@Lon).ToString() + "','" + (@Lat).ToString() + "','" + (@speed).ToString() )", myConnection);


    SqlDataAdapter sadp = new SqlDataAdapter();
    sadp.SelectCommand = myCommand;

    myConnection.Open();
    DataSet DS = new DataSet();

    myCommand.ExecuteNonQuery();

    myConnection.Close();
}

after passing the data from android ..nothing return in back .. and there is no data in the database!! Is there any library missing in the CS file!! I cannot figure out what is the problem.

If you need to have REST use WCF REST instead of ASP.NET web services. There is no REST possibility in ASMX services.

ASFAIK It is not possible to call a .net Web service through REST. Because Web serivce are deployed on SOAP over HTTP. For this kind of requirement, you need to use some third party libraries like KSOAP. Once check this thread

Perhaps the easiest solution for you would be to enable JSON on the web service. Basically it is just putting some changes in the web config file. Take a look here: http://www.codegain.com/articles/aspnet/howto/prepare-a-json-web-service-and-access-it-with-jquery.aspx . The code samples is for jquery, but it will be really easy to create json object from android.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM