【发布时间】:2014-02-21 21:06:03
【问题描述】:
我不是一个非常专业的程序员,但对于考试文本,我必须做一个与 php 服务器交互的 android 应用程序。我阅读了很多教程和示例,但我编写的代码不起作用。你能告诉我为什么吗?
PHP 代码
<?php
$conn = mysqli_connect("localhost","root","","my_onceuponatimestories");
if (mysqli_connect_error()) {
echo("connessione fallita!!".mysqli_connect_error());
exit;
} else {
$sql= "SELECT * ";
$sql.="FROM stories";
//$sql.="WHERE ";
// echo $sql;
//echo "<br>";
$result=mysqli_query($conn,$sql);
if ($result){
if (mysqli_num_rows($result)>0){
//echo mysqli_num_rows($result);
//echo "<br>";
while($e=mysqli_fetch_assoc($result)){
$output = array(htmlspecialchars($e['title']), htmlspecialchars($e['author']));
//$output = array(htmlspecialchars($e['title']), htmlspecialchars($e['author']));
//echo json_encode($output);
}
//echo(json_encode($ris));
}
}
mysqli_close($conn);
}
?
还有我的java代码:
public void getData (View v){
HttpGet httpGet = new HttpGet("http://www.onceuponatimestories.altervista.org/viewAnd.php");
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity= null;
String response = "NADA";
try {
HttpResponse httpReponse =httpClient.execute(httpGet);
httpEntity = httpReponse.getEntity();
response = EntityUtils.toString(httpEntity);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//} catch (UnsupportedEncodingException e) {
//e.printStackTrace();
}
TextView result = (TextView) findViewById(R.id.textView1);
result.setText(response);;
}
【问题讨论】:
-
您希望这段代码做什么以及它做了什么?到目前为止,您尝试过什么来解决这个问题?
标签: php android mysql json database-connection