Calling a WebService Rest with a Post Order

0

I wanted to do on Android a call to a Rest WCF WebService via POST requests.

In C # I can, by sending the post, url and body param . But in Android , whenever I send a parameter, the webservice method (although I can connect), says that data is 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://192.168.1.25/webservice/metodoPOST

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

On the webservice side of WCF Rest, the method signature is as follows:

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

How do I send a POST request without sending named parameters?

    
asked by anonymous 14.04.2016 / 10:49

1 answer

0

Miguel, to send the data you should use OutputStream . So try using the following:

Switch:

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

By:

OutputStream os = request.getOutPutStream();
os.write(user.getBytes());
os.flush();
os.close();

To get the response code:

int responseCode = request.getResponseCode();
    
15.04.2016 / 04:41