Webview Data Mismatch between Android and HTML page
android, jquery, php, webview
Solution
At first write a javascript in the html like this
<script>
function reloadRes(var data){
$('#sample').html( 'hello' );
$('#rest_mesgs').append( data )
.listview( 'refresh' );
}
</script>
then rewrite Http
public class HttpPostID extends AsyncTask<Void,Integer,String> {
static String result;
private static final String webphp = "http://...../queryRestaurantsList.php";
String IDs;
HttpPostID(String ids){
this.IDs =ids;
}
@Override
protected String 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);
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(String data){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
webview.evaluateJavascript("javascript:reloadRes(data)",
null);
}
else
{
webview.loadUrl("javascript:reloadRes(data)");
}
}
}
I am an android developer, so I am not sure about the function reloadRes(var data), please modify it as per your requirement.
Problem
I have a jQuery List View in my Android webview that loads data from the server. I use a HTTP GET response to get data from the server-database. I use this in `<body onload="loadRes()">`. This is my method: ``` <script> lastRecord=0; function loadRes(){ $('#sample').html( 'hello' ); $.get( "queryRestaurantsList.php?lastRecord="+lastRecord, function( data ) { $('#rest_mesgs').append( data ) .listview( 'refresh' ); }); } </script> ``` However, i want to sort this list in a particular order. For that i send a POST message from Android code to the server which contains a string object like this: ``` String list_order ="0012,0002,0003,0001"; ``` This `list_order` String object represent `RestaurantID` in the database which is also the primary key. The format of the this primary key is `VARCHAR(4)`. This is my php file: However, i always get the message "Still working" on my web view even though i can see the HTML of the sorted list in LogCat though HttpResponse ``` <? mysql_connect($host,$username,$password); mysql_select_db($database) or die( "Unable to select database"); echo $_POST['ids']; if($_POST != null){ $query = "SELECT RestaurantID,Name,Short_Loc,Image_Link,Full_Link,Type FROM Restaurant_Info ORDER BY FIND_IN_SET(RestaurantID,'".$ids."')"; $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 "Still working"; } ?> ``` HttpPostID.class - Class that POST data to server ``` public class HttpPostID extends AsyncTask<Void,Integer,Void> { 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; } } ``` I call this in my Activity: `new HttpPostID(tester).execute();` `tester` is a String object. LogCat (only a few lines since output is large) ``` 07-12 02:25:14.850: I/System.out(10440): <li id="0013" onclick="newAct(0013)"><a href=""><img style="height:80px;" src="http://cedars.hku.hk/sections/campuslife/images/pacific.jpg"><h2 style="text-wrap : normal" >Pacific Coffee Company</h2><p id="type" class="ui-li-aside"><strong>Sandwiches, Cut cakes, Pies, Pastries, Muffins, Premium Ground Coffee, Fruit Juices</strong></p><p>Main Campus</p></a></li><li id="0003" onclick="newAct(0003)"><a href="#page4"><img style="height:80px;" ```