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.
我是否需要在某处允许外部连接?或者我正在尝试做什么本质上是错误的?
您的模拟器上的localhost不是桌面上的localhost.在您的桌面上,你需要运行PHP服务器与PHP -S 10.0.2.2:8080(如果它是你的IP).而不是在您的应用程序中使用WebView从模拟器访问该IP.您无法从模拟器访问桌面的localhost(至少没有直接访问).不要仅在localhost上启动服务器.
| 归档时间: |
|
| 查看次数: |
2416 次 |
| 最近记录: |