我将我的android连接到MySQL数据库,它工作正常。我用一个php文件来连接数据库并进行查询。 现在我想显示来自同一数据库的不同表中的数据。我想我必须调用函数读取,但我不知道如何刮削数据。 也许使用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));
?>
谢谢大家
答案 0 :(得分:0)
查看Link。 可以忽略AsyncTask,如示例中所示。
答案 1 :(得分:0)
问题出在PHP文件中:
print(json_encode($records));
必须先去:
mysql_close($con);
JSON无法理解,但它失败了,但更改顺序正常工作