无法通过安卓设备连接本地服务器

Posted

技术标签:

【中文标题】无法通过安卓设备连接本地服务器【英文标题】:Unable to connect with local server via android device 【发布时间】:2015-02-13 07:32:40 【问题描述】:

安装 XAMPP 服务器,创建数据库并成功从 chrome 浏览器和 android 模拟器访问它。但是,每当我尝试从我的三星设备运行应用程序时,它都会拒绝连接我的 IP 地址。

我的 IP 地址没有在 DNS 中注册。

我试过了: 1) 从 url- 127.0.0.1 和 localhost 访问 2) 修改 httpd.conf 和 httpd-xampp.conf 文件 - 更改为需要所有授予的权限 3)卸载我的防病毒软件 4) 更改防火墙和代理设置 5) 在防火墙高级安全中创建新的入站规则 6)重新输入INTERNET权限 7) 写了 httpPost.setHeader("Accept", "application/json");在代码中

我仍然收到以下错误:

  W/IInputConnectionWrapper(22232): getSelectedText on inactive InputConnection
  W/IInputConnectionWrapper(22232): setComposingText on inactive InputConnection
  W/IInputConnectionWrapper(22232): getSelectedText on inactive InputConnection
  W/IInputConnectionWrapper(22232): setComposingText on inactive InputConnection
  D/request!(22232): starting
  W/System.err(22232): org.apache.http.conn.HttpHostConnectException: Connection to
  http://192.XXX.X.XXX refused
  W/System.err(22232):  at   org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
W/System.err(22232):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
W/System.err(22232):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
W/System.err(22232):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
W/System.err(22232):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
W/System.err(22232):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
W/System.err(22232):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
W/System.err(22232):    at com.example.mysqltest.JSONParser.makeHttpRequest(JSONParser.java:111)
W/System.err(22232):    at com.example.mysqltest.Login$AttemptLogin.doInBackground(Login.java:121)
W/System.err(22232):    at com.example.mysqltest.Login$AttemptLogin.doInBackground(Login.java:1)
W/System.err(22232):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
W/System.err(22232):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
W/System.err(22232):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
W/System.err(22232):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
W/System.err(22232):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
W/System.err(22232):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
W/System.err(22232):    at java.lang.Thread.run(Thread.java:856)
W/System.err(22232): Caused by: java.net.ConnectException: failed to connect to /192.168.0.114 (port 80): connect failed: ETIMEDOUT (Connection timed out)
W/System.err(22232):    at libcore.io.IoBridge.connect(IoBridge.java:114)
W/System.err(22232):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
W/System.err(22232):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:460)
W/System.err(22232):    at java.net.Socket.connect(Socket.java:832)
W/System.err(22232):    at `enter code here`org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
W/System.err(22232):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
W/System.err(22232):    ... 16 more
W/System.err(22232): Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
W/System.err(22232):    at libcore.io.Posix.connect(Native Method)
W/System.err(22232):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
W/System.err(22232):    at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
W/System.err(22232):    at libcore.io.IoBridge.connect(IoBridge.java:112)
W/System.err(22232):    ... 21 more
E/Buffer Error(22232): Error converting result java.lang.NullPointerException
E/JSON Parser(22232): Error parsing data org.json.JSONException: End of input at character 0 of 
W/dalvikvm(22232): threadid=12: thread exiting with uncaught exception (group=0x41180438)
E/AndroidRuntime(22232): FATAL EXCEPTION: AsyncTask #1
E/AndroidRuntime(22232): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(22232):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
E/AndroidRuntime(22232):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
E/AndroidRuntime(22232):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
E/AndroidRuntime(22232):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
E/AndroidRuntime(22232):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
E/AndroidRuntime(22232):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
E/AndroidRuntime(22232):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
E/AndroidRuntime(22232):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
E/AndroidRuntime(22232):    at java.lang.Thread.run(Thread.java:856)
E/AndroidRuntime(22232): Caused by: java.lang.NullPointerException
E/AndroidRuntime(22232):    at com.example.mysqltest.Login$AttemptLogin.doInBackground(Login.java:125)
E/AndroidRuntime(22232):    at com.example.mysqltest.Login$AttemptLogin.doInBackground(Login.java:1)
E/AndroidRuntime(22232):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
E/AndroidRuntime(22232):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
E/AndroidRuntime(22232):    ... 5 more
I/Choreographer(22232): Skipped 1143 frames!  The application may be doing too much work on its main thread.
E/WindowManager(22232): Activity com.example.mysqltest.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41bea8a8 that was originally added here
E/WindowManager(22232): android.view.WindowLeaked: Activity com.example.mysqltest.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41bea8a8 that was originally added here
E/WindowManager(22232):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:419)
E/WindowManager(22232):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:322)
E/WindowManager(22232):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:234)
E/WindowManager(22232):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:153)
E/WindowManager(22232):     at android.view.Window$LocalWindowManager.addView(Window.java:557)
E/WindowManager(22232):     at android.app.Dialog.show(Dialog.java:277)
E/WindowManager(22232):     at com.example.mysqltest.Login$AttemptLogin.onPreExecute(Login.java:103)
E/WindowManager(22232):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
E/WindowManager(22232):     at android.os.AsyncTask.execute(AsyncTask.java:534)
E/WindowManager(22232):     at com.example.mysqltest.Login.onClick(Login.java:77)
E/WindowManager(22232):     at android.view.View.performClick(View.java:4191)
E/WindowManager(22232):     at android.view.View$PerformClick.run(View.java:17229)
E/WindowManager(22232):     at android.os.Handler.handleCallback(Handler.java:615)
E/WindowManager(22232):     at android.os.Handler.dispatchMessage(Handler.java:92)
E/WindowManager(22232):     at android.os.Looper.loop(Looper.java:137)
E/WindowManager(22232):     at android.app.ActivityThread.main(ActivityThread.java:4960)
E/WindowManager(22232):     at java.lang.reflect.Method.invokeNative(Native Method)
E/WindowManager(22232):     at java.lang.reflect.Method.invoke(Method.java:511)
E/WindowManager(22232):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
E/WindowManager(22232):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
E/WindowManager(22232):     at dalvik.system.NativeStart.main(Native Method)"

【问题讨论】:

【参考方案1】:

请尝试检查您 PC 的本地 IP 地址。将手机连接到同一网络(WIFI)。然后使用以下地址/端口连接到您的 PC。

在安卓中:

网址:your_pc_local_ip

PORT:80(但我认为这不是强制性的)

我以这种方式使用,它对我来说工作正常。

【讨论】:

谢谢布莱希。它的工作!但是,我的应用程序通过同一网络(WIFI)运行我如何通过另一个网络访问它? 不幸的是我不知道。对不起。 Blehi,还有一个问题.. 现在它说“java.net.SocketException: The operation timed out at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocketImpl”怎么办?跨度> 我不知道到底是什么问题,不幸的是我的笔记本电脑死了,所以我不能告诉你我是如何建立连接的。我记得我尝试使用 apache 和 volley 并且它工作正常。当它得到修复时,我会更新我的答案。我希望不会太晚。 Np。它得到了修复。实际上,由于网络的变化,我的 IP 地址发生了变化。感谢您的回复布莱希。 :)

以上是关于无法通过安卓设备连接本地服务器的主要内容,如果未能解决你的问题,请参考以下文章

adb命令无法连接安卓设备

离线时无法建立与本地主机的连接

我无法从网络中的其他设备访问本地主机的原因是啥?

wifi和3G能在安卓上同时工作吗?

物联网MATLAB通过MQTT与阿里云和本地服务器建立连接

关闭连接后无法通过本地端口连接