gpt4 book ai didi

android - Webview Localhost Connection 拒绝使用 10.0.2.2 地址

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:52:24 25 4
gpt4 key购买 nike

我只是在 Android 模拟器上制作一个基本的 Webview 应用程序,无法连接到我计算机上托管的网站。

这是我的代码:

Android list :

<?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"/>

主要 Activity 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 作为模拟器。

当我运行我的应用程序时,它会在主 Activity 页面上返回 net::ERR_CONNECTION_REFUSED。

我需要在某处允许外部连接吗?或者我正在尝试做的事情本身就存在问题吗?

最佳答案

模拟器上的本地主机不是桌面上的本地主机。在您的桌面上,您需要使用 php -S 10.0.2.2:8080 运行 php 服务器(如果这是您的 IP)。而不是在您的应用程序中使用 WebView 从模拟器访问该 IP。您无法从模拟器访问桌面的本地主机(至少不能直接访问)。不要仅在本地主机上启动服务器。

关于android - Webview Localhost Connection 拒绝使用 10.0.2.2 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27613752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com