通过 android 连接本地主机 - 连接到 10.0.2.2 被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20015883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:09:21  来源:igfitidea点击:

Connecting localhost via android - Connection to 10.0.2.2 refused

androideclipseapache

提问by abdfahim

I have an apache server installed in my PC where I host one PHP file.

我在我的 PC 上安装了一台 apache 服务器,我在其中托管了一个 PHP 文件。

Then I tried to connect to that file using eclipse, but it always gives me below error

然后我尝试使用 eclipse 连接到该文件,但它总是给我以下错误

connection to http://10.0.2.2:8080 refused.

I tried changing address to the followings, but got similar error all the time.

我尝试将地址更改为以下内容,但一直遇到类似的错误。

http://10.0.2.2
http://127.0.0.1
http://localhost

Can anyone please help me.

谁能帮帮我吗。

EDITED: Just for information, I can access to remoter server (e.g. www.mydomain.com) without any problem.

编辑:仅供参考,我可以毫无问题地访问远程服务器(例如 www.mydomain.com)。

CLASS FILE:

类文件:

HttpClient httpclient = new DefaultHttpClient();
Log.d("test","t0");
HttpPost httppost = new HttpPost("http://10.0.2.2:8080/mylibman/data.php");
Log.d("test","t1");
HttpResponse response = httpclient.execute(httppost); // error here
Log.d("test","t2");

Android Manifest:

安卓清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.migrationdesk.mylibman"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.INTERNET" />

Error LogCat:

错误日志:

11-16 02:02:20.803: E/log_tag(1427): Error in http connection org.apache.http.conn.HttpHostConnectException: Connection to http://10.0.2.2:8080 refused
11-16 02:02:20.803: E/log_tag(1427): Error converting result java.lang.NullPointerException: lock == null
11-16 02:02:20.803: E/log_tag(1427): Error parsing data org.json.JSONException: End of input at character 0 of 

回答by Coderji

alright go to Apache httpd.conf (located in Apache folder): and search for

好的,转到 Apache httpd.conf(位于 Apache 文件夹中):并搜索

Order Deny,Allow
Deny from all
Allow from 127.0.0.1

and check if the second line is Deny, if it is then change it to:

并检查第二行是否为拒绝,如果是,则将其更改为:

Allow from all

then restart the Appache server, and tell me the feadback.

然后重新启动 Appache 服务器,并告诉我反馈。

Edit

编辑

try it out in your real device:

在您的真实设备中尝试一下:

go to CMD and type ipconfigunder IPv4 take the IP address and change the IP it will look similar to this:

转到 CMD 并ipconfig在 IPv4 下输入 IP 地址并更改 IP,它看起来类似于:

http://192.168.0.106:8080/mylibman/data.php // similar to this.

Turn Off the firewall and any anti-virus application in your PC

关闭 PC 中的防火墙和任何防病毒应用程序

and please give me the feedback.

请给我反馈。

回答by Junior Mayhé

Mikehelped me a lot on this issue. So here my steps to avoid this Connection refused.

迈克在这个问题上帮了我很多。所以这里我的步骤是避免这种连接被拒绝

1) Add if to test Android SDK. It seems that in new versions you have to explicity allow the connection to be open in a different thread:

1) 添加if来测试Android SDK。似乎在新版本中,您必须明确允许在不同线程中打开连接:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            System.out.println("*** My thread is now configured to allow connection");
        }

...

...

2) Add an exception for your port:

2)为您的端口添加一个例外:

in your case allow inbound connection for port 8080

在您的情况下,允许端口的入站连接 8080

3) test your connection by browsing the desired url locally

3) 通过在本地浏览所需的 url 来测试您的连接

use your browser and try to visit http://10.0.2.2:8080locally

使用您的浏览器并尝试在http://10.0.2.2:8080本地访问

4) Add in your manifest file a permission for internet:

4)在您的清单文件中添加互联网权限:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.yourapp"
    android:versionCode="1"
    android:versionName="1.1" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.INTERNET"/>

I am using Wamp, and its apache is listening to servername localhost and port 8080 (allow from all in httpd.conf).

我正在使用 Wamp,它的 apache 正在侦听服务器名称 localhost 和端口 8080(允许来自 httpd.conf 中的所有内容)。

My local IP is something similar to 192.168.0.XXX/folder/my-page-here

我的本地 IP 类似于 192.168.0.XXX/folder/my-page-here

回答by Mr.Moustard

You can use your real IP address to reach your virtual server. Take a look at my answer here. I hope it helps.

您可以使用您的真实 IP 地址来访问您的虚拟服务器。看看我在这里的回答。我希望它有帮助。

回答by seby598

I fix this problem by writing my IP address from cmd -> ipconfigin the url :

我通过cmd -> ipconfig在 url 中写入我的 IP 地址来解决这个问题:

 HttpPost httppost = new HttpPost( "http://192.168.x.x/mylibman/data.php" );

回答by Nimisha Patel

if your SDK version is less then 9 then add above code for SDK version them some changes in Apache update httpd.conf as line deny from all..replace that sentence Allow from all and restart services of WAMP server then use 10.0.2.2address or 10.0.2.2:8080port no and include permission line in android AndroidManifest file

如果您的 SDK 版本小于 9,则为 SDK 版本添加上面的代码,它们在 Apache 更新 httpd.conf 中进行一些更改,作为 line deny from all.. 替换那句话 Allow from all 并重新启动 WAMP 服务器的服务,然后使用10.0.2.2地址或10.0.2.2:8080端口号和在 android AndroidManifest 文件中包含权限行

<uses-permission 
android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">
</uses-permission>