2

I'm tired of giving turns to something that seems so simple but it is not to give and have scoured all forums and tututiais .

I wanted to do in android a call to a WCF Rest WebService through POST requests .

In c# I can do it, sending the post , url and "body param" ."

But in android ( java ) when sending a parameter , the webservice method (although I can not connect ) , dizme that data as always invalid .

The code I have so far is this , which is the main connection method :

public void LoginWS(final String urlWS, final String user) throws Exception 
{
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() 
        {
            try 
            {
               //urlWS ="http://mywebsite/webservicename.svc/rest/methodnamePost";

                String inputCoded = Base64.encodeToString(user.getBytes("UTF-8"), Base64.NO_WRAP);
                HttpURLConnection request = (HttpURLConnection) new URL(urlWS).openConnection();

                try {
                    request.setDoOutput(true);
                    request.setDoInput(true);
                    request.setRequestProperty("Content-Type", "application/json");
                    request.setRequestMethod("POST");                        
                    request.connect();

                    InputStream is = request.getInputStream();
                    String resp = convertStreamToString(is);
                    byte[] data = Base64.decode(resp, Base64.NO_WRAP);
                    response = new String(data, "UTF-8");

                    is.close();


                } finally {
                    request.disconnect();
                }
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
    });

    thread.start();
}

In the webservice side, the method signature is this:

[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json,      ResponseFormat = WebMessageFormat.Json)]
string methodnamePost(string input);

If someone can help me to understand how to send a POST request sending parameters without a name would be great.

Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80

0 Answers0