我想通过PHP,JSON从Android读取MYSQL数据库中的数据。 我一直在尝试很多不同的例子,但我的Android手机无法读取任何数据。 当我运行加载应用:(如何从Android读取MYSQL数据库?
- MySQL数据库信息
数据库名称后的应用程序我的手机没有做任何事情:PeopleData 表名:人 表有:ID,姓名,性别,birthyear
- PHP源:我测试的index.php,结果是正确的
-index.php
<?php
mysql_connect("127.0.0.1","root","");
mysql_select_db("PeopleData");
$q=mysql_query("SELECT * FROM people WHERE birthyear>1980");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();
?>
Android的Java进行读取数据库
package com.json; 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; import android.widget.Toast; public class JSON extends Activity { TextView resultView; HttpClient client; JSONObject json; // the year data to send @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.json); resultView = (TextView) findViewById(R.id.tvjson); client = new DefaultHttpClient(); new Read().execute("text"); } public JSONObject RedData() throws ClientProtocolException,IOException,JSONException { HttpPost httppost = new HttpPost("http://127.0.0.1/series/database/index.php"); HttpResponse r = client.execute(httppost); int status = r.getStatusLine().getStatusCode(); if (status == 200) { HttpEntity e = r.getEntity(); String data = EntityUtils.toString(e); JSONArray jArray = new JSONArray(data); JSONObject last = jArray.getJSONObject(0); // 0 -> the last object return last; } else { Toast.makeText(JSON.this, "error", Toast.LENGTH_LONG); return null; } } public class Read extends AsyncTask<String, Integer, String> { @Override protected String doInBackground(String... arg0) { // TODO Auto-generated method stub try { json = RedData(); 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); } } }
xml文件
<TextView android:id="@+id/tvjson" android:layout_width="match_parent" android:layout_height="wrap_content" > </TextView>
清单
<uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.permission.INTERNET"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".JSON" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
控制台
[2012-02-22 16:19:58 - json] Android Launch! [2012-02-22 16:19:58 - json] adb is running normally. [2012-02-22 16:19:58 - json] Performing com.json.JSON activity launch [2012-02-22 16:19:58 - json] Automatic Target Mode: Unable to detect device compatibility. Please select a target device. [2012-02-22 16:19:59 - json] Uploading json.apk onto device '01499EF80D00D009' [2012-02-22 16:19:59 - json] Installing json.apk... [2012-02-22 16:20:01 - json] Success! [2012-02-22 16:20:01 - json] Starting activity com.json.JSON on device 01499EF80D00D009 [2012-02-22 16:20:02 - json] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.json/.JSON }
设备
doing nothing
我不知道我能做些什么了。请有人帮助我。 谢谢。
IP地址127.0.0.1指的是本地设备,因此对于一个HTTP请求不会离开设备/仿真器。 – 2012-02-23 00:26:20
安全方面,这通常是一个非常糟糕的想法,因为Android和mySQL数据库之间没有保护,您必须将访问凭据公开给移动设备。你通常希望在客户端和数据库之间有一些API层 – 2012-02-23 00:27:01
就像Pekka所说的,与Android的mysql数据库通信的最佳/安全的方法是构建一个API。含义:创建一个Web应用程序/ api,用于控制与数据库的交谈(使用getter和setter,或任何您需要的),然后从Android通过您的web api与数据库通信。 – Shattuck 2012-02-23 00:59:36