Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 811
engg_mike Avatar asked Dec 23 '14 03:12

engg_mike


1 Answers

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.

like image 135
gorlok Avatar answered Sep 21 '22 15:09

gorlok