0

What i am trying to do is display the response from a POST message on to the HTML loaded in my webview. However, the my webview appears blank. I can see the response message by in LogCat by printing it out. However, again my webview appears blank. Example.html is the page loading in my webview. My implementation is below:

HttpPostID.java - class that sends a POST message to server

public class HttpPostID extends AsyncTask<Void,Integer,Void> {

    public static String result;

    private static final String webphp = "http://..../queryRestaurantsList.php";

    String IDs;

    HttpPostID(String ids){
        this.IDs =ids;
    }

    @Override
    protected Void doInBackground(Void... params){

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(webphp);

        try {

            String hello="111,222,333";

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("order",IDs));
            nameValuePairs.add(new BasicNameValuePair("hello",hello));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpclient.execute(httppost);
            HttpEntity resEntity = response.getEntity();

            result = EntityUtils.toString(resEntity);

            //System.out.println(result);


        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void v){
        System.out.println(result);
        Restaurant_view.myWebView.loadUrl("javascript:loadData('"+result+"')");

    }
}

MainActivity.java

public static WebView myWebView;

protected void onCreate(Bundle savedInstanceState) {

    myWebView = (WebView) findViewById(R.id.webView3);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);
    myWebView.addJavascriptInterface(this,"Android");

    myWebView.loadUrl(webaddress);

    new HttpPostID(tester).execute();
   }

example.html

<head>
<script>
function loadData(str){
        var c = document.getElementById("rest_mesgs");
        c.innerHTML = "";
        c.innerHTML = str;
        }
</script>
</head>
<body style="background-color : #e9e9e9;">
<div id="rest_mesgs">
</div>
</body>

.php File on the Server

mysql_connect($host,$username,$password);
mysql_select_db($database) or die( "Unable to select database");

//Check if POST array is empty or not
$order = $_POST['order'];

if ($order != null){

$query = "SELECT RestaurantID,Name,Short_Loc,Image_Link,Full_Link,Type FROM Restaurant_Info
          ORDER BY FIND_IN_SET(RestaurantID,'".$order."')";

$result = mysql_query($query) or die( "Unable to execute query");


while ($row = mysql_fetch_array($result)){
        print '<li id="'.$row['RestaurantID'].'" onclick="newAct('.$row['RestaurantID'].')">';  
        print '<a href="'.$row['Full_Link'].'">';
        print '<img style="height:80px;" src="'.$row['Image_Link'].'">';
        print '<h2 style="text-wrap : normal" >'.$row['Name'].'</h2>';
        print '<p id="type" class="ui-li-aside"><strong>'.$row['Type'].'</strong></p>';
        print '<p>'.$row['Short_Loc'].'</p>';
        print '</a>';
        print '</li>';
        }
}
else {
    echo "Waiting for POST";
}   
stud91
  • 1,854
  • 6
  • 31
  • 56
  • PLs show ur HTML page? U need to add some CSS – Maveňツ Jul 14 '14 at 13:11
  • @Mann see example.html that is the page loading into webview – stud91 Jul 14 '14 at 13:12
  • make text color black and background color white – Maveňツ Jul 14 '14 at 13:14
  • @Mann text color is black and this background is very light-ish grey – stud91 Jul 14 '14 at 13:16
  • wht does this line do Restaurant_view.myWebView.loadUrl("javascript:loadData('"+result+"')"); ???? – Maveňツ Jul 14 '14 at 13:18
  • @Mann doesn't this call the function loadData(str) in javascript? Or could you tell me any other way? – stud91 Jul 14 '14 at 13:20
  • pls do check this http://stackoverflow.com/questions/24731932/calling-android-function-using-jscript – Maveňツ Jul 14 '14 at 13:30
  • @Mann This is the opposite of what i am trying to do. This calls a java (android) function from the javascript/HTML. I am trying to call a javascript function from java (Android). – stud91 Jul 14 '14 at 13:32
  • 1
    `new HttpPostID(tester).execute();` You have this call to early i think. Only start the task when the page is fully loaded. Override onPageFinished() of WebViewClient http://stackoverflow.com/questions/3149216/how-to-listen-for-a-webview-finishing-loading-a-url-in-android – greenapps Jul 14 '14 at 13:53
  • @greenapps Yes thanks. It does work in `onPageFinished()` method when i use a simple string like `String abc ="abc"` and `webview.loadUrl("javascript:loadData('"+abc+"')");` BUT when i add my response (Please see my `php` code) it appears blank. – stud91 Jul 15 '14 at 22:26
  • My god you got it working already. So your response is empty. So why did you say 'when i add my response it appears blank' where your response is already empty or null. What has that to do with javascript? First tell: what is your response exactly? Empty? – greenapps Jul 16 '14 at 06:12
  • So it works with a 'simple' string and it does not work with a 'complicated' string? That's all? Then find out what makes it complicated. Make the simple sting bigger and bigger. Especially try characters like < > " ' and so on. Was that all? – greenapps Jul 16 '14 at 06:37

0 Answers0