1

I have this code:

     @Override
public void onCreate(Bundle savedInstanceState) {
    try {
        super.onCreate(savedInstanceState);
        InetAddress ip;
        mWebview  = new WebView(this);
        mWebview.getSettings().setJavaScriptEnabled(true);
        final Activity activity = this;
        String ipv4add;
        mWebview.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
            }
        });
        ip = InetAddress.getLocalHost();
        ipv4add = ip.getHostAddress().toString();
        System.out.println(ipv4add);
        mWebview .loadUrl(ipv4add+"/Lab4/Task1/index.php");
            mWebview.getSettings().setLoadsImagesAutomatically(true);
            mWebview.getSettings().setJavaScriptEnabled(true);
            mWebview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        setContentView(mWebview );
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

So what it does exactly is first: it should get the server ip that the phone is supposed to be connected to, then after that it will be inserted into the URL so that the phone can connect to the localserver and access my php files. However, when I launch this into my android phone it just crashes. Why does it do so? Hoping you guys can help me solve this.

Paradigm
  • 159
  • 1
  • 3
  • 11

1 Answers1

0

You don't have to dynamically request for your server's IP. Or do you? What you can do is get the static IP address of your server (by checking your server's IP configuration) and change to this

mWebview .loadUrl("http://your.ip.address.here/Lab4/Task1/index.php");

and remove your

ip = InetAddress.getLocalHost();
ipv4add = ip.getHostAddress().toString();

Once you are ready to deploy to live, the 192.168.1.x will have to be replaced with your live domain such as

mWebview .loadUrl("http://www.yourdomain.com/Lab4/Task1/index.php");

Also, don't forget this in your manifest:

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

user1506104
  • 6,554
  • 4
  • 71
  • 89