我将我的 android 连接到 MySQL 数据库,它工作正常。我使用 php 文件来连接数据库并进行查询。现在我想显示来自同一个数据库的不同表的数据。我想我必须调用函数 read 但我不知道如何删除数据。也许使用 Web 服务更容易或更好?这是我在另一篇文章中使用的代码
package com.example.onlinetestactivity;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
TextView resultView;
HttpClient client;
JSONObject json;
String Dat;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultView = (TextView) findViewById(R.id.tvjson);
client = new DefaultHttpClient();
try {
json = RedData();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Dat = json.toString();
new Read().onPostExecute(Dat);
}
public JSONObject RedData() throws ClientProtocolException, IOException, JSONException {
HttpPost httppost = new HttpPost("http://file.php");
HttpResponse r = client.execute(httppost);
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
JSONArray jArray = new JSONArray(data);
JSONObject last = jArray.getJSONObject(0);
return last;
}
public class Read extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
json = RedData();
//Toast.makeText(OnlineTestActivity.this, json.getString(arg0[0]), Toast.LENGTH_LONG);
return json.getString(arg0[0]);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String data) {
// TODO Auto-generated method stub
resultView.setText(data);
}
}
}
这是php文件
<?php
header('Content-type: application/json');
$server = "XXXXXXXX";
$username = "XXXXXXX";
$password = "XXXXXXXX";
$database = "XXXXXXX";
$con = mysql_connect($server, $username, $password) or die ("No se puede conectar: " . mysql_error());
$db_selected = mysql_select_db($database, $con);
if (!$db_selected)
{
die ("Error al seleccionar la Base de datos: " . mysql_error());
}
$sql = "select XXXXXXXXXXX)";
$result = mysql_query($sql) or die ("Query error: " . mysql_error());
$records = array();
while($row = mysql_fetch_assoc($result)) {
$records[] = $row;
}
mysql_close($con);
print(json_encode($records));
?>
谢谢大家