-1

I have an android application that's supposed to send a request to a simple HelloWorld C# webservice I made on ServiceStack but I am not able to connect. My application crashes when I try to connect. Here is my code on Eclipse, trying to access the ServiceStack service:

String base = "http://192.168.1.7:62938/json/reply/Hello?Name=";
    String str = editTextField.getText().toString();
    StringBuilder url = new StringBuilder(base + str);
    String result = "";

HttpClient hc = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url.toString());
    HttpResponse r = hc.execute(httpget);
    int status = r.getStatusLine().getStatusCode();
    if (status == 200) {
        HttpEntity e = r.getEntity();
        String data = EntityUtils.toString(e);
        JSONObject o = new JSONObject(data);
        result= o.getString("result");
    } 

My C# service code for ServiceStack:

//Request DTO
public class Hello
{
    public string Name { get; set; }
}

//Response DTO
public class HelloResponse
{
    public string Result { get; set; }
    public ResponseStatus ResponseStatus { get; set; } 
}

//Can be called via any endpoint or format, see: //http://mono.servicestack.net/ServiceStack.Hello/
public class HelloService : Service
{
    public object Any(Hello request)
    {
        return new HelloResponse { Result = "Helloooo, " + request.Name };
    }
}

My service works fine on my laptop when I go to localhost:62938/json/reply/Hello?Name="arbitraryName" but it does not work when I try to replace localhost with my laptop's ip address and access the service from an android device. It also does not work if I replace localhost with my IP address and try it on my browser on my laptop. Note: I want to make it work from a real android device, not an emulator.

Is there something different with ServiceStack services where I cannot access it normally from another device? I have already tried opening port 62938 and it did not work.

I appreciate any guidance. Thank you.

user3445268
  • 39
  • 1
  • 11

2 Answers2

1

It also does not work if I replace localhost with my IP address and try it on my browser on my laptop.

If you have tried accessing the ServiceStack service through the local IP address of 192.168.1.7 in your computer's web browser and it is also unreachable, then the issue isn't isolated to Android.

This is issue is likely the result of one or more of these problems:

Check you are listening on the correct IPs:

Your ServiceStack service isn't configured to listen for requests on any other interfaces other than localhost. Select your relevant hosting option:

Self Hosting:

This can happen if you are self hosting and you have configure the app host to start with appHost.Start("http://localhost:62938/");. You would need to replace localhost with a + symbol to have it listen on all local addresses.

IIS Express:

By default IIS Express is used by Visual Studio during development, unless manually configured to use IIS, and is restricted to localhost requests only. You should see this answer as to how to configure IIS Express to allow non-local access as well.

This tutorial by Scott Hanselman is also very good, and provides great step-by-step instructions for configuring IIS Express.

IIS:

You can confirm the IP addresses that you server is configure to listen on by following these instructions. They provide instructions for both IIS6 and IIS7+.

Firewall:

Your computer may have a firewall preventing you accessing that port, or accepting outside traffic. Note your firewall may be built into antivirus software you run. You should add an exception rule for http traffic on port 62938.

Correct IP:

You are trying to access on 192.168.1.7. You should confirm that IP address is in fact correct. Most home networks are configured to provide a dynamic IP address by the network router. The IP address may have changed since you last checked it. You should try running a ping to the IP from your development machine.


Until you can successfully access the service through your web browser on your development machine, at the local network IP starting 192.168.1.X then I wouldn't attempt to access from Android. It's not an Android issue if other systems can't access your service also.

I hope that helps. If you provide more information about your specific environment, I may be able to provide more specific instructions. If I had to guess, I would say IIS Express issue.


Edit:

Now that you can access the service in the web browser of your android device but not in the application, we know the service is remotely accessible. This means your connectivity issue is isolated now to your application. The first thing I would check, is that your application has permission to make network requests. In your AndroidManifest.xml you need to ensure that android.permission.INTERNET is included

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

If you have that permission then you should be able to make the request successfully. If it continues to fail, then the reason need to be diagnosed from the exception that is causing the application to crash. In which case you should wrap the data request in a try { ... } catch(Exception exception) { } and log the exception.

Community
  • 1
  • 1
Scott
  • 21,211
  • 8
  • 65
  • 72
  • Hi Scott, thank you for your help. I have downloaded IIS Express and followed the directions at: http://gilesey.wordpress.com/2013/04/21/allowing-remote-access-to-your-iis-express-service/ I can now access http:///portNumber/json/reply/Hello?name="john" from my phone browser, but I still can't make an HTTP request to it somehow when I run my android application on a device. Any other ideas? – user3445268 Mar 21 '14 at 22:47
  • @user3445268 You're welcome. Glad you have it working now in the browser, you are certainly getting closer to having it working. I have updated my answer at the end with further suggestions. I hope that helps. Let me know how you get on. Thanks – Scott Mar 22 '14 at 09:33
  • @user3445268 Would appreciate your feedback. Thanks – Scott Mar 26 '14 at 20:47
  • Thanks Scott, I eventually got my android to be able to call my .Net service, but only from the emulator. Calling it from an actual android device was difficult since my service was on my machine's localhost, and to make my .Net service public so that external devices could access it seems too complicated for me. I read Scott Hanselman's tutorial on making my .Net service public and it was so many steps and hard for me to not make mistakes. – user3445268 Mar 27 '14 at 23:23
  • @user3445268 Did you get this working? I'd like to close this question if I can. – Scott Aug 16 '14 at 08:54
0

As the Android emulator is considered to be running on a different device, to refer to the loopback IP (127.0.0.1) on our local development machine we need to use the special 10.0.2.2 alias.

Other special device IP's can be found in Andorid's documentation.

mythz
  • 141,670
  • 29
  • 246
  • 390