0

I am trying to call web service api from android app, code shown below:

ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("check", "chk"));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(HTTP_CHECK);
post.setEntity(new UrlEncodedFormEntity(nameValuePair));
httpClient.execute(post);
HttpResponse httpres = httpClient.execute(post);

This is my web service API on asp.net

    [WebMethod]
    public string HelloWorld2(String check)
    {
        return "SuccessCheck";
    }

Host Server is Godaddy. The above method worked previously. But now it gives the error on an app -

System.InvalidOperationException:Missing Parameter:check.

How it says that parameter is missing. The parameter is passed as namevaluepair. I don't know what the problem is? However, it works perfectly on localhost.

Service methods which do not have any parameters work well on a server as well as on local host. Now when I change the android code as below

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(HTTP_CHECK + "?check=chk");
httpClient.execute(post);
HttpResponse httpres = httpClient.execute(post);

Now it works well on a server as well as host. As I am passing the parameter with URL as given above and not using a name-value pair.

What is the problem in the first scenario and why it suddenly changed? Please help.

RAJ JOHRI
  • 31
  • 5
  • 1
    in your first android code you create the "ArrayList nameValuePair" but never use it anywhere when doing the actual Http calls. – stamanuel Mar 12 '17 at 10:54
  • here you can see how it's used: http://stackoverflow.com/questions/17607589/what-is-the-use-of-listnamevaluepair-or-arraylistnamevaluepair – stamanuel Mar 12 '17 at 10:56
  • Thanks for your comments. But I have used it. Result is not yet working. Its very strange. – RAJ JOHRI Mar 12 '17 at 13:07
  • then please update your question with the real code, cause in your snippet it's still missing – stamanuel Mar 12 '17 at 13:10

1 Answers1

1

In the first scenario you are not using nameValuePair anywhere.

Add this bit:

post.setEntity(new UrlEncodedFormEntity(nameValuePair)); before you call httpClient.execute(post);

zaitsman
  • 8,984
  • 6
  • 47
  • 79
  • No this is not the answer as I have used it. I only forgot to mention it on my question however I have used it in my origianl source. Please help – RAJ JOHRI Mar 12 '17 at 13:01