Webview Localhost Connection拒绝使用10.0.2.2地址

eng*_*ike 8 apache android localhost webview connection-refused

我只是在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"/>
Run Code Online (Sandbox Code Playgroud)

主要活动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");
}
Run Code Online (Sandbox Code Playgroud)

终端(在本地主机端口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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

我使用Mamp和AVD作为模拟器.

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

我是否需要在某处允许外部连接?或者我正在尝试做什么本质上是错误的?

gor*_*lok 6

您的模拟器上的localhost不是桌面上的localhost.在您的桌面上,你需要运行PHP服务器与PHP -S 10.0.2.2:8080(如果它是你的IP).而不是在您的应用程序中使用WebView从模拟器访问该IP.您无法从模拟器访问桌面的localhost(至少没有直接访问).不要仅在localhost上启动服务器.

  • `10.0.2.2`是模拟器使用的IP,见链接/sf/ask/686599231/ -web-server-instead-of-using-computer。所以你的假设是不正确的,也是你的答案 (3认同)

小智 0

查找此文件 ports.conf 并根据需要添加 Listen 8080 并重新启动服务器。