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

时间:2016-05-02 18:06:21

标签: java android client-server

我有一个Spring Restful Server功能。它接受来自任何浏览器和我用来测试它的Java客户端的请求。

问题是当我尝试使用Android连接到它时,无论是在模拟器上还是在智能手机上。

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

完美无缺。如果我调试它,我看到con有HTTP响应代码200,页面的响应正是john的点数。

但如果我在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());
}

这一行总是突破:

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

如果我调试它,我看到HTTP响应代码是-1,而不是200。 我已经尝试了我的内部网络IP(在我的情况下为192.168.1.79),但它不起作用。我已经在Stackoverflow中搜索了很多线程,并尝试了很多用户拥有的代码,在所有这些代码中,它总是在相同的代码中突破,即obj.openConnection()。

我在清单中获得了互联网的许可,但不会因为这个原因。

它可能是什么?

编辑:这是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) 

1 个答案:

答案 0 :(得分:0)

尝试使用Asynctask包装requestServer()。在doInBackground()

中执行此方法