我只是在Android模拟器上制作基本的Webview应用程序,无法连接到我的计算机上托管的网站。
这是我的代码:
Android Manifest:
<?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。
我是否需要在某处允许外部连接?或者我试图做的事情本质上是错误的吗?
答案 0 :(得分:6)
localhost它不是桌面上的localhost。在您的桌面上,你需要运行PHP服务器与PHP -S 10.0.2.2:8080(如果它是你的IP)。而不是在您的应用程序中使用WebView从模拟器访问该IP。您无法从模拟器访问桌面的localhost(至少没有直接访问)。不要仅在localhost上启动服务器。
答案 1 :(得分:0)
查找此文件ports.conf并在必要时添加Listen 8080并重新启动服务器。
答案 2 :(得分:0)
使用10.0.2.2
是正确的,无论如何都没有错。您可以在下面的答案中看到原因
问题可能与您的应用程序仅关注127.0.0.1
而非所有接口有关。您需要确保使用下面的内容
php -S 0.0.0.0:8080
我也看到了你的赏金问题,这也解答了你需要运行你的Django服务器,如下所示
python manage.py runserver 0.0.0.0:8000
PS:下次你的帖子是赏金的@kingraphaII,要善待人们,不要只是鬼魂
答案 3 :(得分:0)
对我来说有效的是用我的PC笔记本电脑192.168.2.7替换本地主机地址。 @gorlok的评论帮助我实现了解决方案。