Android 通过PHP连接到MySQL,JSON错误
我编写了一个simpe PHP脚本,用于打印JSONECODE$输出。它输出带有前导和结尾[]的JSON。当我通过Emulator运行我的Android程序时,它声明:JSONObject文本必须在[…]的字符1处以“{”开头,然后列出我的JSON,因此我知道它获取数据,解码过程存在一个问题,即它没有删除这些方括号。下面是将错误抛出日志的函数:Android 通过PHP连接到MySQL,JSON错误,android,Android,我编写了一个simpe PHP脚本,用于打印JSONECODE$输出。它输出带有前导和结尾[]的JSON。当我通过Emulator运行我的Android程序时,它声明:JSONObject文本必须在[…]的字符1处以“{”开头,然后列出我的JSON,因此我知道它获取数据,解码过程存在一个问题,即它没有删除这些方括号。下面是将错误抛出日志的函数: public class JSONfunctions { public static JSONObject getJSONfromURL(Str
public class JSONfunctions {
public static JSONObject getJSONfromURL(String url){
InputStream is = null;
String result = "";
JSONObject jArray = null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
jArray = new JSONObject(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return jArray;
}
}听起来您的JSon响应像:[{'name':'value'},…] JSon对象必须以{开头,如错误消息所示。它应该是: {'my_array':[……]}
然后您可以编写:new JSONObject.getJSONArraymy_array;,但是PHP中默认的printjsonencode$output;的格式没有标题和方括号,无法更改。