apache 使用www.example.com地址的Webview本地主机连接被拒绝10.0.2.2

qyzbxkaa  于 2023-02-19  发布在  Apache
关注(0)|答案(5)|浏览(133)

我只是在android模拟器上做了一个基本的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。
我需要在某个地方允许外部连接吗?或者我正在尝试做的事情本质上有什么问题吗?

5kgi1eie

5kgi1eie1#

localhost在你的模拟器上它不是localhost在你的桌面上.在你的桌面上你需要运行php服务器与php -S 10.0.2.2:8080(如果这是你的IP).然后访问该IP从模拟器与WebView在你的应用.你不能访问桌面的localhost从模拟器(没有直接至少).不要启动你的服务器只在localhost上.

r8uurelv

r8uurelv2#

查找ports.conf文件,如果需要,添加Listen8080,然后重新启动服务器。

cnwbcb6i

cnwbcb6i3#

无论如何,使用10.0.2.2是正确的,没有错。您可以在下面的答案中看到原因
why do we use 10.0.2.2 to connect to local web server instead of using computer ip address in android client
这个问题可能与您的应用程序只侦听127.0.0.1而不是所有接口有关。

php -S 0.0.0.0:8080

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

python manage.py runserver 0.0.0.0:8000

附言:下次你发布赏金@kingraphaII时,要友善地回应人们,不要只是一个幽灵

hof1towb

hof1towb4#

对我有效的是用我的个人电脑笔记本电脑www.example.com替换localhost地址192.168.2.7,在我的例子中,@gorlok评论帮助我找到了我的解决方案。

n53p2ov0

n53p2ov05#

Android Emulator是一个单独的设备。Emulator不是您的计算机的一部分。Emulator无法访问您的本地服务器。
因此,模拟器返回net::ERR_CONNECTION_REFUSED错误。
我找到了一个解决这个问题的简单方法。
获取我的本地IP地址,如192.168.99.112
要获取您的IP地址,请打开您的终端/cmd,键入ipconfig
确保您在AndroidManifest文件中的互联网权限

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

然后

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

相关问题