Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL with parameter in WebView not working in Android?

Tags:

android

I am trying to call the loadUrl method in a webview with the below url

http://stage.realtylog.net/iPhone/functions.php?username=xxx&ID=xxx&act=readFileAndPrint

When calling this URL, it gives an image from server. I need to display that image on a webview.

It's working fine with normal URLs. But its not working with the above url with parameters.

It gives me an error like:

Function name must be a string in /var/www/stage/realtylog.net/iPhone/functions.php

I tried to URLEncode the above url , but its still not working. Any kind of help will be highly appreciated.

package com.hussain.webview2;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.SslErrorHandler;
import android.net.http.*;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class WebViewDemo2Activity extends Activity
{
   final Activity activity = this;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
       super.onCreate(savedInstanceState);
       this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
       setContentView(R.layout.main);

       WebView webView = (WebView) findViewById(R.id.webview);
      // webView.setWebViewClient(new WebViewClient());
      // webView.addView(webView.getZoomControls());
      webView.getSettings().setJavaScriptEnabled(true);

      webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress)
        {
            activity.setTitle("Loading...");
            activity.setProgress(progress * 100);

            if(progress == 100)
                activity.setTitle("Done");
        }
    });

    webView.setWebViewClient(new WebViewClient() {
        @Override
       public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
       {
           // Handle the error
       }



        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);
            return true;
        }
    });

    //webView.loadUrl("http://developer.android.com");
    webView.loadUrl("http://stage.realtylog.net/iPhone/functions.php?username=xxxx&ID=xxxx&act=readFileAndPrint");


    }
}
like image 886
mH16 Avatar asked Feb 11 '12 06:02

mH16


4 Answers

Welcome to a horrible known bug, that Google doesn't feel like fixing, or even addressing.

http://code.google.com/p/android/issues/detail?id=17535

like image 98
Dino Fancellu Avatar answered Nov 11 '22 06:11

Dino Fancellu


I searched a lot to solve this issue but found nothing working. Though I only encountered this problem with android < 4.0

I only encountered this problem when first loading the url. If the webview has already loaded some other url it works fine.

So this is my workaround that works although it is really silly.

        if (API_ICS_OR_LATER) {

            mWebView.loadUrl(mURL);


        } else {
            /***
             * Workaround for Webview bug when url contains parameters
             * https://code.google.com/p/android/issues/detail?id=17535
             */

            mWebView.loadData("<html></html>", "text/html", "UTF-8");

            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    mWebView.loadUrl(mURL);

                }
            }, 500);
        }
like image 21
Elyess Abouda Avatar answered Nov 11 '22 08:11

Elyess Abouda


change your url to:

StringBuffer buffer=new StringBuffer("http://stage.realtylog.net/iPhone/functions.php");
buffer.append("?username="+URLEncoder.encode("xxxxxxx"));
buffer.append("&id="+URLEncoder.encode("xxxxxxxx"));
buffer.append("act="+URLEncoder.encode("readFileAndPrint"));
webView.loadUrl(buffer.toString());
like image 7
jeet Avatar answered Nov 11 '22 06:11

jeet


This fixes the issue:

webView.getSettings().setDomStorageEnabled(true);
like image 2
Istiak Morsalin Avatar answered Nov 11 '22 08:11

Istiak Morsalin