Java 如何从Android读取MYSQL数据库?
我想通过PHP从MYSQL数据库读取数据,从Android读取JSON。 我尝试了很多不同的例子,但我的Android手机无法读取任何数据。 当我运行应用程序时,手机在加载应用程序后不会执行任何操作:(Java 如何从Android读取MYSQL数据库?,java,php,android,json,Java,Php,Android,Json,我想通过PHP从MYSQL数据库读取数据,从Android读取JSON。 我尝试了很多不同的例子,但我的Android手机无法读取任何数据。 当我运行应用程序时,手机在加载应用程序后不会执行任何操作:( MYSQL数据库信息 数据库名称:PeopleData 表名:人 表中有:身份证、姓名、性别、出生年份 PHP源代码:我测试了index.PHP,结果是正确的 -index.php <?php mysql_connect("127.0.0.1",&quo
- MYSQL数据库信息
- 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();
?>
- 装置
无所事事
package com.Online.Test;
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 OnlineTestActivity 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.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("//link.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(OnlineTestActivity.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();
//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);
}
}
}
package com.Online.Test;
导入java.io.IOException;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.util.EntityUtils;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.widget.TextView;
导入android.widget.Toast;
公共类OnlineTestActivity扩展活动{
/**在首次创建活动时调用*/
文本视图结果视图;
HttpClient;
JSONObject json;
字符串Dat;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
resultView=(TextView)findViewById(R.id.tvjson);
client=新的DefaultHttpClient();
试一试{
json=RedData();
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
Dat=json.toString();
新的Read().onPostExecute(Dat);
}
publicJSONObject RedData()抛出ClientProtocolException、IOException、JSONException{
HttpPost-HttpPost=newhttppost(“//link.php”);
HttpResponse r=client.execute(httppost);
//int status=r.getStatusLine().getStatusCode();
//如果(状态==200){
HttpEntity e=r.getEntity();
字符串数据=EntityUtils.toString(e);
JSONArray jArray=新JSONArray(数据);
JSONObject last=jArray.getJSONObject(0);//0->最后一个对象
最后返回;
//}其他{
//Toast.makeText(OnlineTestActivity.this,“error”,Toast.LENGTH\u LONG);
//返回null;
//}
}
公共类读取扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…arg0){
//TODO自动生成的方法存根
试一试{
json=RedData();
//makeText(OnlineTestActivity.this,json.getString(arg0[0]),Toast.LENGTH_LONG);
返回json.getString(arg0[0]);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串数据){
//TODO自动生成的方法存根
resultView.setText(数据);
}
}
}
查看要点
- IP地址127.0.0.1表示本地设备,因此 HTTP请求未离开设备/仿真器
- 在服务器端脚本(此处为PHP)中,设置 标题('content-type:application/json;charset=utf-8')
- 在doInBackground的Try块中,检查ReadData()是否为null 不是
- 在JSONArray jArray=newjsonarray(数据);检查jArray是否为null 还是不
IP地址
127.0.0.1
指的是本地设备,因此HTTP请求不会离开设备/仿真器。从安全角度来看,这通常是一个非常糟糕的想法,因为Android和mySQL数据库之间没有保护,您必须向移动设备公开访问凭据。您通常希望有一些这样的凭据客户端和数据库之间API层的rt,如Pekka所说,这是与mysql数据通信的最佳/安全方式
<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 }
package com.Online.Test;
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 OnlineTestActivity 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.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("//link.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(OnlineTestActivity.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();
//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);
}
}
}