qiTsuk February 2016

Consuming Simple WCF from Android

I am writing a simple app, that needs to verify login details through a MySQL database. I specifically cannot use PHP, so I opted for the WCF service model. Now, I have the WCF working through my browser. All of the get methods are working in my browser and returns the desired JSON output, but for good order, I will post the Service Interface;

    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "getUserAuthenticated/{value1}/{value2}")]
    bool GetUserAuthenticated(string value1, string value2);

    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "getIsUserModel/{value}")]
    bool GetIsUserModel(string value);

    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "getIsUserActive/{value}")]
    bool GetIsUserActive(string value);

As stated, this works. But when trying to consume the service from my app like so;

    public void UserAuthenticated(String email, String password) {
        try {
            URL url = new URL("http://10.0.0.180:15021/Service1.svc/GetUserAuthenticated/" + email + "/" + password);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("GET");

            Log.d("Response Code: ", "" + httpURLConnection.getResponseCode());
            InputStream in = new BufferedInputStream(httpURLConnection.getInputStream());
            String response = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
            Log.d("Response String: ", response);

        } catch (MalformedURLException mue) {
            mue.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

      

Answers


qiTsuk February 2016

So I solved it. What I did was;

  1. I opened the port I was attempting to connect on in Windows Firewall. In my case 8008. I opened it both for outgoing and ingoing traffic.
  2. I installed IIS Express, and set up a local page on the IP that my workstation has.
  3. After a lot of rumaging about on the interwebz, and here on Stack Overflow, I found other who had problems connecting to local IIS addresses through Genymotion, even using the standard IP for localhost, 10.0.3.2. From another thread here on SO I found this guide;

https://developer.chrome.com/devtools/docs/remote-debugging

Turns out, I had to port forward the device. And now it is working. I can now connect to my WCF service through my Genymotion emulator on my workstations IP at port 8008.

I hope this helps someone.

Cheers.

Post Status

Asked in February 2016
Viewed 3,324 times
Voted 12
Answered 1 times

Search




Leave an answer