简体   繁体   中英

Webview Localhost Connection refused using 10.0.2.2 address

I'm just making a basic Webview app on an android emulator and cannot connect to a website hosted on my computer.

Here is my code:

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

Main Activity Java file:

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");
}

Terminal (Starting website on local host port 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 File (Under Apache Folder):

#
# 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

I am using Mamp and AVD as the emulator.

When I run my app, it returns net::ERR_CONNECTION_REFUSED on the Main activity page.

Do I need to allow external connections somewhere? OR is there something inherently wrong with what I am trying to do?

localhost on your emulator it's not localhost on your desktop. On your desktop you need to run php server with php -S 10.0.2.2:8080 (if that it's your IP). And than access that IP from the emulator with WebView at your app. You can't access desktop's localhost from the emulator (no directly at least). Don't start your server on localhost only.

查找此文件ports.conf 并在必要时添加Listen 8080 并重新启动服务器。

Using 10.0.2.2 is correct and not wrong in anyway as such. You can see why in the below answer

why do we use 10.0.2.2 to connect to local web server instead of using computer ip address in android client

The issue may be related to your application listening to 127.0.0.1 only and not all interfaces. You need to make sure you use something like below

php -S 0.0.0.0:8080

I saw your bounty question as well, which also answers that you need run your Django server as below

python manage.py runserver 0.0.0.0:8000

PS: And next time your post a bounty @kingraphaII, be kind enough to respond to people and don't just be a ghost

What worked for me was to replace localhost address with my pc laptop, 192.168.2.7, in my case. @gorlok comment helped me towards my solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM