10

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?

Marc Estrada
  • 1,657
  • 10
  • 20
engg_mike
  • 101
  • 1
  • 5

5 Answers5

7

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.

gorlok
  • 1,155
  • 6
  • 16
  • 4
    `10.0.2.2` is a IP used by emulator, see the link https://stackoverflow.com/questions/9808560/why-do-we-use-10-0-2-2-to-connect-to-local-web-server-instead-of-using-computer. So your assumption is incorrect and also your answer – Tarun Lalwani May 22 '18 at 05:43
0

Look for this file ports.conf and add Listen 8080 if necessary and restart the server.

calm
  • 363
  • 2
  • 9
0

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

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • It's true. Android emulator create a custom private network. 10.0.2.2 will point to your localhost's desktop. There is something more here then. Anyways, I would start the server listening on all networks (0.0.0.0) too. I would try connecting using desktop IP, just to see if that works. More about emulator private network: https://developer.android.com/studio/run/emulator-networking – gorlok May 22 '18 at 14:15
0

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.

0

Android Emulator is a separate device. Emulator isn't part You Computer. Emulator cann't access your Local Server.

So Emulator return net::ERR_CONNECTION_REFUSED error.

I found a Simpal way to solve the issue.
Get my local IP address as like 192.168.99.112
To get your IP address open your Tarminal/cmd type ipconfig

Ensure your internet permission in AndroidManifest File

<uses-permission android:name="android.permission.INTERNET"/>

Then

WebView browser = (WebView)findViewById(R.id.webview);
webView.loadUrl("http://192.168.99.112/projectName");

If you wanna run laravel server try this

php artisan serve --host=192.168.99.112 --port=8000
robiul awal
  • 1
  • 1
  • 2