简体   繁体   English

Android客户端未连接到REST服务器

[英]Android client not connecting to REST server

I have a Spring Restful Server functional. 我有一个Spring Restful Server功能。 It accepts requests from any browser and from a Java client that I made to test it. 它接受来自任何浏览器和我测试它的Java客户端的请求。

The problem is when I try to connect to it with Android, whether it is on the emulator or on a smartphone. 问题是当我尝试通过Android连接到它时,无论它是在模拟器上还是在智能手机上。

I have this simple code in my Java client to test the server: 我的Java客户端中有以下简单代码来测试服务器:

public void requestServer() throws IOException {
    String url = "http://localhost:8080/checkPoints?username=john";

    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    // optional default is GET
    con.setRequestMethod("GET");

    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'GET' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    //print result
    System.out.println(response.toString());
}

It works perfectly. 它运作完美。 If I debug it I see that con has the HTTP response code 200, and the response from the page is exactly the number of points that john has. 如果我对其进行调试,我会看到con的HTTP响应代码为200,而页面中的响应正是john所拥有的点数。

But if I try it on Android with the same code to be activated when a button is clicked: 但是,如果我在Android上尝试使用单击按钮时要激活的相同代码进行尝试:

public void requestServer(View view) throws IOException {
    String url = "http://10.0.2.2:8080/checkPoints?username=joao";

    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    // optional default is GET
    con.setRequestMethod("GET");

    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'GET' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    //print result
    System.out.println(response.toString());
}

It always breaks out in this line: 它总是在这一行中出现:

HttpURLConnection con = (HttpURLConnection) obj.openConnection();

If I debug it, I see that the HTTP response code is -1, instead of 200. I've tried my internal network IP (192.168.1.79 in my case), and it does not work. 如果我对其进行调试,则会看到HTTP响应代码是-1,而不是200。我已经尝试了内部网络IP(在我的情况下为192.168.1.79),但它不起作用。 I've searched in a lot of threads here in Stackoverflow and tried a lot of code that users had, and in all those pieces of code, it always breaks out in the same code, which is that obj.openConnection(). 我在Stackoverflow中搜索了很多线程,并尝试了许多用户拥有的代码,在所有这些代码中,它总是在同一代码中出现,即obj.openConnection()。

I have the permission for Internet in the manifest, it won't be because of that. 我已在清单中获得了Internet的许可,这不是因为这个。

What could it be? 会是什么呢?

Edit: Here is the stackTrace 编辑:这是stackTrace

5-02 19:19:38.109 2769-2769/pt.androidrestfulclient E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                        Process: pt.androidrestfulclient, PID: 2769
                                                                                        java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                                            at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:289)
                                                                                            at android.view.View.performClick(View.java:4780)
                                                                                            at android.view.View$PerformClick.run(View.java:19866)
                                                                                            at android.os.Handler.handleCallback(Handler.java:739)
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                            at android.os.Looper.loop(Looper.java:135)
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                                            at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                                                         Caused by: java.lang.reflect.InvocationTargetException
                                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                                            at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                            at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
                                                                                            at android.view.View.performClick(View.java:4780) 
                                                                                            at android.view.View$PerformClick.run(View.java:19866) 
                                                                                            at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                            at android.os.Looper.loop(Looper.java:135) 
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                                            at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
                                                                                         Caused by: android.os.NetworkOnMainThreadException
                                                                                            at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
                                                                                            at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
                                                                                            at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
                                                                                            at libcore.io.IoBridge.connect(IoBridge.java:122)
                                                                                            at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
                                                                                            at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
                                                                                            at java.net.Socket.connect(Socket.java:882)
                                                                                            at com.android.okhttp.internal.Platform.connectSocket(Platform.java:174)
                                                                                            at com.android.okhttp.Connection.connect(Connection.java:152)
                                                                                            at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276)
                                                                                            at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
                                                                                            at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
                                                                                            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:332)
                                                                                            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:500)
                                                                                            at pt.ulisboa.tecnico.cmov.androidrestfulclient.MainActivity.requestServer(MainActivity.java:50)
                                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                                            at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                            at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) 
                                                                                            at android.view.View.performClick(View.java:4780) 
                                                                                            at android.view.View$PerformClick.run(View.java:19866) 
                                                                                            at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                            at android.os.Looper.loop(Looper.java:135) 
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                                            at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

Try wrapping requestServer() with an Asynctask. 尝试使用Asynctask包装requestServer() Execute this method in doInBackground() 在doInBackground()中执行此方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM