使用10.0.2.2地址时,Webview本地主机连接被拒绝。

10

我只是在安卓模拟器上创建了一个基础的Webview应用程序,但无法连接到我的电脑上托管的网站。

以下是我的代码:

Android清单:

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

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

主活动Java文件:

public class MainActivity extends Activity {
private WebView webView;
@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");
    }
    setContentView(R.layout.activity_main);

    webView = (WebView) findViewById(R.id.webView);
    webView.loadUrl("http://10.0.2.2:8080");
}

终端(在本地主机端口8080上启动网站):

Michaels-MacBook-Pro-5:web michael$ php -S localhost:8080
PHP 5.5.14 Development Server started at Mon Dec 22 14:08:01 2014
Listening on http://localhost:8080

httpd.conf文件(在Apache文件夹下):

#
# This should be changed to whatever you set DocumentRoot to.
#
<Directory "/Applications/MAMP/htdocs">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
#   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important.  Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
Options All

#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
#   Options FileInfo AuthConfig Limit
#
AllowOverride All

#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all

我正在使用Mamp和AVD仿真器。

当我运行我的应用程序时,在主活动页面返回net::ERR_CONNECTION_REFUSED错误。

我需要在某个地方允许外部连接吗?还是我尝试做的事情本质上有问题?


请打开您的php服务器http.conf文件,并允许远程访问。 - Noor Nawaz
那个文件在哪里?在Mamp->conf->php5.6.2下,我只看到pear.conf和php.ini。我不确定这些文件是用来做什么的。感谢您的帮助。 - engg_mike
请问您能否检查一下以下位置的配置文件: "/Applications/MAMP/conf/apache/httpd.conf" - Ciril
我也看不到MAMP在这里的作用?你正在启动运行在8080端口上的PHP开发服务器,那么MAMP在这里有什么关联呢? - Tarun Lalwani
@kingraphaII,你还没有给任何人提供评论或反馈。开这样一个悬赏的意义是什么? - Tarun Lalwani
显示剩余3条评论
5个回答

7

在模拟器上,localhost并不是指你的桌面电脑。在桌面电脑上,你需要使用php -S 10.0.2.2:8080(如果这是你的IP地址)来运行php服务器,并通过应用程序中的WebView访问该IP地址。你不能直接从模拟器访问桌面电脑的localhost(至少不能直接访问)。不要仅在localhost上启动服务器。


4
10.0.2.2 是模拟器使用的 IP 地址,请参考链接 https://dev59.com/wGkw5IYBdhLWcg3wkLQX。因此,您的假设是不正确的,您的答案也是不正确的。 - Tarun Lalwani

0

使用10.0.2.2是正确的,没有任何错误。您可以在下面的答案中看到原因。

为什么在Android客户端中连接本地Web服务器时要使用10.0.2.2而不是计算机IP地址

问题可能与您的应用程序仅监听127.0.0.1有关,而不是所有接口。您需要确保使用类似以下内容:

php -S 0.0.0.0:8080

我也看到了你的悬赏问题,它回答了你需要如下运行Django服务器的问题

python manage.py runserver 0.0.0.0:8000

顺便说一下,下次如果你发布悬赏 @kingraphaII,请友善地回应人们,不要只是一个幽灵。


没错。Android模拟器会创建一个自定义的私有网络。10.0.2.2将指向您本地主机的桌面。这里还有更多内容。无论如何,我会在所有网络上(0.0.0.0)启动服务器监听。我会尝试使用桌面IP连接,只是为了看看是否有效。关于模拟器私有网络的更多信息:https://developer.android.com/studio/run/emulator-networking - gorlok

0

对我有用的是将本地主机地址替换为我的笔记本电脑地址,即192.168.2.7,在我的情况下。@gorlok的评论帮助我找到了解决方案。


0
请查找文件ports.conf,如有必要,请添加Listen 8080并重新启动服务器。

0

Android模拟器是一个独立的设备。模拟器不是您计算机的一部分。模拟器无法访问您的本地服务器。

因此,模拟器会返回net::ERR_CONNECTION_REFUSED错误。

我找到了一个简单的解决方法。
获取我的本地IP地址,例如192.168.99.112
要获取您的IP地址,请打开Tarminal/cmd并键入ipconfig

确保在AndroidManifest文件中具有Internet权限

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

那么

WebView browser = (WebView)findViewById(R.id.webview);
webView.loadUrl("http://192.168.99.112/projectName");

如果你想运行 Laravel 服务器,请尝试这个。
php artisan serve --host=192.168.99.112 --port=8000

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接