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.

4
  • 1
    in your first android code you create the "ArrayList<NameValuePair> nameValuePair" but never use it anywhere when doing the actual Http calls. Commented Mar 12, 2017 at 10:54
  • here you can see how it's used: stackoverflow.com/questions/17607589/… Commented Mar 12, 2017 at 10:56
  • Thanks for your comments. But I have used it. Result is not yet working. Its very strange. Commented Mar 12, 2017 at 13:07
  • then please update your question with the real code, cause in your snippet it's still missing Commented Mar 12, 2017 at 13:10

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.