我要做的是将POST消息的响应显示在我的webview中加载的HTML上。但是,我的webview显示为空白。我可以通过将其打印出来在LogCat中看到响应消息。但是,我的webview再次显示为空白。
Example.html
是我在网页浏览中加载的页面。
我的实现如下:
HttpPostID.java - 向服务器发送POST消息的类
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文件
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";
}