0

I am trying make a call to a Web Service (asmx) with parameters but I keep getting a "Missing parameter: Lane" response. the code below is what i am using

final HttpClient Client = new DefaultHttpClient();
        String jsonstring = "", step = "0";
        try { 
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("Lane", "1"));
            params.add(new BasicNameValuePair("Name", "Test test")); 

            HttpPost httppostreq = new HttpPost(url);  

            httppostreq.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpresponse = Client.execute(httppostreq);

            jsonstring = EntityUtils.toString(httpresponse.getEntity());

I am able to call the Web Serviceusing Postman when I add the parameters in the header and set the "Content Type:application/x-www-form-urlencoded" but can't make it work in Android Studio

enter image description hereenter image description here

This is the code in my webservice:

   [WebMethod]
    public void MyTestService(string Lane, string Name)
    {
            Dictionary<string, object> parameter = new Dictionary<string, object>();
            parameter.Add("@lane", Lane);
            parameter.Add("@name", Name);

            DataSet dt = DataAccess.executeStoreProcedureDataSet("sp_GetLaneName", parameter);
            string jsonReturned = JsonConvert.SerializeObject(dt, Formatting.Indented);
            Context.Response.ContentType = "text/plain; charset=UTF-8";
            Context.Response.Write(jsonReturned);           
    }
Jocast80
  • 55
  • 7
  • Tip: You should try [Retrofit](https://stackoverflow.com/questions/30180957/send-post-request-with-params-using-retrofit) – Pratik Butani Mar 11 '20 at 13:18
  • I got it working concatenating the parameters to the URL. I don't think this is the correct way to do it. Will try to use retrofit as Pratik suggested – Jocast80 Mar 11 '20 at 16:02
  • I ended up adding Retrofit and it is working now. – Jocast80 Mar 12 '20 at 20:11

1 Answers1

0

Add Header to your request below

httppostreq.setHeader(HTTP.CONTENT_TYPE,
                    "application/x-www-form-urlencoded;charset=UTF-8");
Milan Pansuriya
  • 2,521
  • 1
  • 19
  • 33