stu*_*d91 3 php jquery android webview
我的Android webview中有一个jQuery List View,它从服务器加载数据.我使用HTTP GET响应从服务器数据库获取数据.我用它<body onload="loadRes()">.这是我的方法:
<script>
lastRecord=0;
function loadRes(){
$('#sample').html( 'hello' );
$.get(
"queryRestaurantsList.php?lastRecord="+lastRecord,
function( data ) {
$('#rest_mesgs').append( data )
.listview( 'refresh' );
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
但是,我想按特定顺序对此列表进行排序.为此,我从Android代码发送一条POST消息到服务器,其中包含一个像这样的字符串对象:
String list_order ="0012,0002,0003,0001";
Run Code Online (Sandbox Code Playgroud)
此list_orderString对象RestaurantID在数据库中表示,它也是主键.这个主键的格式是VARCHAR(4).这是我的php文件:但是,我总是在我的网页视图上收到"仍在工作"的消息,即使我可以通过HttpResponse看到LogCat中已排序列表的HTML
<?
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";
}
?>
Run Code Online (Sandbox Code Playgroud)
HttpPostID.class - 将数据POST到服务器的类
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我在我的Activity中调用它:new HttpPostID(tester).execute(); tester是一个String对象.
LogCat(由于输出很大,只有几行)
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;"
Run Code Online (Sandbox Code Playgroud)
小智 5
首先在html中写一个像这样的javascript
<script>
function reloadRes(var data){
$('#sample').html( 'hello' );
$('#rest_mesgs').append( data )
.listview( 'refresh' );
}
</script>
Run Code Online (Sandbox Code Playgroud)
然后重写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)");
}
}
Run Code Online (Sandbox Code Playgroud)
}
我是一个android开发人员,所以我不确定函数reloadRes(var data),请根据你的要求修改它.
| 归档时间: |
|
| 查看次数: |
751 次 |
| 最近记录: |