php - Android 和 HTML 页面之间的 Webview 数据不匹配

标签 php android jquery webview

我的 Android web View 中有一个 jQuery ListView ,用于从服务器加载数据。我使用 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> 

但是,我想按特定顺序对列表进行排序。为此,我从 Android 代码向服务器发送了一条 POST 消息,其中包含如下字符串对象:

String list_order ="0012,0002,0003,0001";

list_order字符串对象表示 RestaurantID在数据库中也是主键。这个主键的格式是VARCHAR(4) .这是我的 php 文件: 但是,即使我可以通过 HttpResponse 在 LogCat 中看到排序列表的 HTML,我总是在我的 Web View 上收到消息“仍在工作”

<?
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 - 将数据发布到服务器的类

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;
    }
}

我在 Activity 中调用它:new HttpPostID(tester).execute(); tester是一个字符串对象。

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;" 

最佳答案

首先像这样在html中写一个javascript

 <script>
 function reloadRes(var data){
    $('#sample').html( 'hello' );
    $('#rest_mesgs').append( data )
        .listview( 'refresh' );
}
</script>

然后重写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)");
    }

    }

本人是android开发者,对reloadRes(var data)函数不是很清楚,请按您的要求修改。

关于php - Android 和 HTML 页面之间的 Webview 数据不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24657383/

相关文章:

php - 从 Angularjs 渲染 Twig 模板

javascript - 关于谷歌地图的关键API

android - 使用opensl es和android ndk实时播放音频

jquery - 在 jQuery 中显示 JSON

javascript - textfield.value = ""在 Firefox 中不起作用,但在 Safari/Chrome 中起作用

javascript - 检查多个较短的字符串,JS?

php - 使用mysql从特定用户生成带有php的html树

php - 使用 php api 登录多个 Instagram 帐户

android - 如何在 Activity 结果方法上检测应用内购买的状态

java - Activity 关闭后显示 AlertDialog