0

But connect to my localhost web server from Android Emulator is working fine. I used the OAUTH for security and used Spring RESTful Web Service in web server.

My android stuff for connection config:

    public static final String API_HOST = "http://192.168.x.x:8080/"; //this is my pc ip address, (test with 10.0.2.2 but no work)
    public static final String API_BASE_URI = API_HOST + "MyProject/api/v1/";
    public static final String API_OAUTH_URI = API_HOST + "MyProject/oauth/";
    public static final String API_CLIENT_ID = "abrer112";
    public static final String API_CLIENT_SECRET = "a12101!#*$JE4323";
    public static final int API_HTTP_STATUS_SUCCESS = 200;
    public static final int API_HTTP_STATUS_UNAUTHORISED = 401;
    public static final int API_HTTP_STATUS_BAD_REQUEST = 400;
    public static final int API_HTTP_STATUS_UNPROCESSABLE_ENTITY = 422;
    public static final String API_OAUTH_HEADER_AUTH_PREFIX = "Bearer ";

    // ROLES
    public static final String ROLE_ADMIN = "ADMIN";
    public static final String ROLE_MANAGER = "MANAGER";
    public static final String ROLE_USER = "USER";

Server settings:
my xml file:

<!-- Protect API URIs with ROLES -->
    <sec:http pattern="/api/v1/**" create-session="never"
        entry-point-ref="oauthAuthenticationEntryPoint" auto-config="true"
        use-expressions="true">
        <sec:anonymous enabled="false" />
        <sec:intercept-url pattern="/api/v1/admin/**" access="hasRole('ADMIN')" />
        <sec:intercept-url pattern="/api/v1/manager/**"
            access="hasRole('MANAGER') or hasRole('ADMIN')" />
        <sec:intercept-url pattern="/api/v1/user/**"
            access="hasRole('USER') or hasRole('MANAGER') or hasRole('ADMIN')" />
        <sec:custom-filter ref="resourceServerFilter"
            before="PRE_AUTH_FILTER" />
        <sec:access-denied-handler ref="oauthAccessDeniedHandler" />
    </sec:http>

Java file: eg:

@RestController
@RequestMapping("/user/passwords")
public class ChangePasswordController {

All connection and settings are very fine and I can always connect this web server from Android Studio Emulator but can't connect from Android app and Android Studio USB Devices. Is there something I missed to do? Any help appreciated.

Note: possible duplicate at this ?? But I can't figure out the solution. Appreciated for any help!

Community
  • 1
  • 1
Ye Win
  • 2,020
  • 14
  • 21
  • Are you using the wifi to connect in the network or usb tethered? – Enzokie Nov 08 '16 at 03:29
  • @Enzokie with usb tethered but I try also wifi and not work – Ye Win Nov 08 '16 at 03:30
  • I can say that USB tethered wont really work so use the wifi always. – Enzokie Nov 08 '16 at 03:30
  • Write a simple API, and Try to access the API using another PC browser or your phone browser working at the same WIFI, which can tell you that the problem is at client or server. – grantonzhuang Nov 08 '16 at 03:33
  • Try oppening any browser in you android device and input `http://192.168.x.x:8080/` , check if that works. Also try removing the `http://` scheme – Enzokie Nov 08 '16 at 03:36
  • @Enzokie, it's not work, do you have any idea?? – Ye Win Nov 08 '16 at 03:46
  • @grantonzhunag, I can access other PC server from my android phone, same project, so it's my pc server problem?? – Ye Win Nov 08 '16 at 03:49
  • Maybe Firewall on your PC causes this problem. – grantonzhuang Nov 08 '16 at 03:52
  • @YeWin yeah it could be your firewall or you use the wrong IP or port. Before testing this stuff it is much advisable if you put some dummy `index.html` for the sake of testing. – Enzokie Nov 08 '16 at 03:55
  • Possible duplicate of [How can I access my localhost from my Android device?](http://stackoverflow.com/questions/4779963/how-can-i-access-my-localhost-from-my-android-device) – Rahul Khurana Nov 08 '16 at 03:57
  • @YeWin try running the steps above without attaching the USB in your PC. – Enzokie Nov 08 '16 at 04:16
  • @Enzokie, turning firewall off also not work for me, now I recheck my network, ipaddress and steps i have to do, thank alot for help, I will let you know the progress when I finished. – Ye Win Nov 08 '16 at 04:20

2 Answers2

1

If your server is running on your desktop, then it isn't a localhost server. A localhost server would be running on the android device itself.

You need to have your server routable over the internet from your android device. The easiest way is to have them on the same wifi network, and to make sure your wifi router doesn't block connections to that port.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • In my android host, will I used the pc ipaddress or localhost or 10.0.2.2?I'm sure it's on same wifi network but not sure for block connections or not. – Ye Win Nov 08 '16 at 03:33
  • You'd always use the PC's IP address. I'm not sure where you got that other IP from, but its just a random IP in the unassigned range. It would almost always be wrong. – Gabe Sechan Nov 08 '16 at 03:35
-1

run ipconfig on cmd, then find IPv4 address of your pc, it is the ip you should use in your application

masoud vali
  • 1,528
  • 2
  • 18
  • 29