0

我正在开发一个涉及调用 RESTful API 的 Android 应用程序。我有用 Python 编写的 REST 服务,在我的笔记本电脑 ( 192.168.156.1) 上的 8080 端口上运行。我的手机通过 Connectify Wifi 连接连接到我的笔记本电脑。电话在192.168.156.112。现在,我可以从手机的浏览器使用 URL 访问该服务,例如http://192.168.156.1:8080/lighting/12345. 但是,当我尝试从应用程序访问它时,我在 LogCat 中收到“拒绝连接”错误。以下是我用来建立连接的代码。

    String sampleURL = "`http://192.168.156.1:8080/lighting/weert`";
        HttpClient Client = new DefaultHttpClient();
         try
         {
             HttpGet httpget = new HttpGet(sampleURL);
             String serverString = "";
             HttpResponse response= Client.execute(httpget);
             StatusLine statusLine = response.getStatusLine();
             if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
                 ByteArrayOutputStream out = new ByteArrayOutputStream();
                 response.getEntity().writeTo(out);
                 out.close();
                 serverString = out.toString();
             } 
             threadMsg(SetServerString);
          }
        catch(Exception ex){
            Log.e("RESTCALL",ex.getMessage());  
            threadMsg("Fail!");
         }

threadMsg() 是将响应字符串传递回 UI 线程的方法。

解决此问题的任何帮助都会很棒!提前致谢!

更新:添加堆栈跟踪

    I/ActivityManager(  246): START {act=android.intent.action.MAIN cat=   [android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.testreceiver/.MainActivity u=0} from pid 464
    I/ActivityManager(  246): Displayed com.example.testreceiver/.MainActivity: +315ms
    W/System.err(15799): org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.156.1:8080 refused
    D/AudioHardware(   89): AudioHardware pcm playback is going to standby.
    D/AudioHardware(   89): closePcmOut_l() mPcmOpenCnt: 1
    W/System.err(15799):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
    W/System.err(15799):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
    W/System.err(15799):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
    W/System.err(15799):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
    W/System.err(15799):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
    W/System.err(15799):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
    W/System.err(15799):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 
    W/System.err(15799):    at com.example.testreceiver.MainActivity$1.run(MainActivity.java:47)
    W/System.err(15799):    at java.lang.Thread.run(Thread.java:856) 
    W/System.err(15799): Caused by: java.net.ConnectException: failed to connect to /192.168.156.1 (port 8080): connect failed: EHOSTUNREACH (No route to host)
    W/System.err(15799):    at libcore.io.IoBridge.connect(IoBridge.java:114)
    W/System.err(15799):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
    W/System.err(15799):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
    W/System.err(15799):    at java.net.Socket.connect(Socket.java:842)
    W/System.err(15799):    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
    W/System.err(15799):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
    W/System.err(15799):    ... 8 more
    W/System.err(15799): Caused by: libcore.io.ErrnoException: connect failed: EHOSTUNREACH (No route to host)
    W/System.err(15799):    at libcore.io.Posix.connect(Native Method)
    W/System.err(15799):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
    W/System.err(15799):    at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
    W/System.err(15799):    at libcore.io.IoBridge.connect(IoBridge.java:112)
    W/System.err(15799):    ... 13 more
    E/RESTCALL(15799): Connection to `http://192.168.156.1:8080` refused

此外,这是在服务器上运行的代码。

    import socket
    from bottle import route, run@route('/lighting/<name>', method='GET')

    def hello(name="ABC"):
    return name

    run(host='192.168.156.1', port=8080, debug=True)
4

1 回答 1

1

问题解决了!我停止使用 connectify 热点并切换到路由器,我的手机和笔记本电脑现在已连接到该路由器。

于 2013-09-26T08:04:49.990 回答