我正在尝试从Android中的数据库中检索数据,但没有返回任何数据。我在做什么错,为什么会这样?当我转到getData.php的链接时,出现以下错误:
致命错误:未捕获错误:调用/storage/ssd1/078/5480078/public_html/denuncias/getdata.php:12中的未定义函数mysql_query():堆栈跟踪:#0 {main}抛出于/ storage / ssd1 /第12行的078/5480078 / public_html / denuncias / getdata.php
这是我的代码:
getData.php
<?php
include 'DatabaseConfig.php';
$con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);
if (!$con)
{
echo('Could not connect: ' . mysql_error());
}
$result = mysql_query("SELECT titulo FROM denuncias");
while($row = mysql_fetch_assoc($result))
{
$output[]=$row;
}
print(json_encode($output));
mysql_close($con);
?>
MainActivity.java
package bughunters.tashfik.demo;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
String sData;
TextView tv;
String Data = "";
String result;
InputStream isr;
Context con;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.test);
new getData().execute("");
}
private class getData extends AsyncTask<String, Void, String> {
String name;
@Override
protected String doInBackground(String... params) {
result = "";
isr = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://luisalonsoriveraibarra.000webhostapp.com/denuncias/getdata.php"); //YOUR PHP SCRIPT ADDRESS
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
isr = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
//convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(isr, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json = jArray.getJSONObject(i);
Data=Data+"\n"+ json.getString("Head");
}
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error Parsing Data " + e.toString());
}
return "Executed";
}
@Override
protected void onPostExecute(String result) {
tv.setText(""+Data);
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
}
PS:表名是denuncias,我要显示的行称为titulo。