【发布时间】:2017-09-04 06:18:49
【问题描述】:
此代码从我的 MySQL 服务器获取用户名数据。每当我运行它时,我都会在此JSONArray js=new JSONArray(result); 上收到错误消息。控制台没有提供其他细节。请帮忙。
private String getdata(){
try {
URL url = new URL(address);
HttpURLConnection con= (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
is=new BufferedInputStream(con.getInputStream());
}catch(Exception e){
e.printStackTrace();
}
try {
BufferedReader br=new BufferedReader(new InputStreamReader(is));
StringBuilder sb=new StringBuilder();
while((line=br.readLine())!=null){
sb.append(line+"\n");
}
is.close();
result=sb.toString();
//Log.d("TheRealapp", result);
}catch(Exception e){
e.printStackTrace();
}
try{
JSONArray js=new JSONArray(result);
JSONObject jo = null;
data = new String[js.length()];
for(int i=0;i<js.length();i++){
jo=js.getJSONObject(i);
data[i]= jo.getString("username");
}
} catch (JSONException e) {
e.printStackTrace();
}
return String.valueOf(getdata());
//Log.d("TheRealapp", String.valueOf(data));
}
这是我的服务器端代码:
<?php
ini_set("display_errors", true);
ini_set("html_errors", false);
require "conn.php";
$query=mysqli_query($conn,"SELECT * FROM UserData");
if ($query){
while($row=mysqli_fetch_array($query)){
$flag[] =$row;
}
print(json_encode($flag));
}
mysqli_close($conn);
?>
【问题讨论】:
-
错误信息是什么意思?
-
The console gives no other details我不相信。 -
大家好,我需要更多帮助。这段代码可以工作吗?我现在的那个?