Android在真实设备上读取MYSQL
以下是我的部分代码:Android在真实设备上读取MYSQL,android,mysql,Android,Mysql,以下是我的部分代码: try { // make json string, try also hamburger //String json = "{\"pid\":\"name\",\"price\":\"created_at\",\"updated_at\":3}"; // send as http get request URL url = new URL("http://smsas.eu/dm/get_all_products.php"); UR
try {
// make json string, try also hamburger
//String json = "{\"pid\":\"name\",\"price\":\"created_at\",\"updated_at\":3}";
// send as http get request
URL url = new URL("http://smsas.eu/dm/get_all_products.php");
URLConnection conn = url.openConnection();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
//System.out.println(line);
JSONObject nodeRoot = new JSONObject(line);
JSONArray nodeStats = nodeRoot.getJSONArray("products");
for(int i=0; i < nodeStats.length();i++){
String sSDR = nodeStats.getJSONObject(i).getString("name");
System.out.println(sSDR);
tt.setText(sSDR);
}
}
rd.close();
} catch (Exception e) {
tt.setText("ERR");
e.printStackTrace();
}
试试看{
//制作json字符串,也可以尝试汉堡包
//字符串json=“{\'pid\':\'name\',\'price\':\'created\'u at\',\'updated\'u at\':3}”;
//作为http get请求发送
URL=新URL(“http://smsas.eu/dm/get_all_products.php");
URLConnection conn=url.openConnection();
//得到回应
BufferedReader rd=新的BufferedReader(新的InputStreamReader(conn.getInputStream());
弦线;
而((line=rd.readLine())!=null){
//系统输出打印项次(行);
JSONObject nodeRoot=新的JSONObject(行);
JSONArray nodeStats=nodeRoot.getJSONArray(“产品”);
对于(int i=0;i
当我在虚拟设备上运行应用程序时,它是工作的,但当我在手机上运行时——不是。我已经添加了internet权限。您是否在单独的线程中执行internet请求?你会遇到什么错误?不,所有的东西都在mainActivity中,几乎都是代码。这就是你的答案,你需要在另一个线程上执行冗长的操作。互联网就是其中之一。看看这里:谢谢,在你的链接中找到了解决方案。