通过PHP连接MySQL,JSON错误
我编写了一个简单的 PHP 脚本来打印 jsonencode($output)。它输出带有前导和结尾 [] 的 JSON。当我通过模拟器运行我的 Android 程序时,它指出: A JSONObject 文本必须以 '{' 开头 [..... 然后列出我的 JSON,所以我知道它正在获取数据,有一个问题解码过程不会删除那些方括号。这是将错误抛出到日志的函数:
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;
}
}
I wrote a simpe PHP script that prints an jsonencode($output). It outputs the JSON with a leading and ending []. When I run my Android program through the emulaotor, it states: A JSONObject text must begin with '{' at character 1 of [..... then lists my JSON, so I know its getting the data, there is a problem with the decode process that its not removing those square brackets. Here is the function that is throwing the error to the log:
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您的 JSon 响应如下所示:
[{'name':'value'},...]
您的 JSon 对象必须以
{
开头,如错误消息所示。应该是:{'my_array':[.......]}
然后你可以这样写:
new JSONObject().getJSONArray("my_array");
It sounds like your JSon response looks like:
[{'name':'value'},...]
Your JSon Object must begin with
{
like the error message says. It should be:{'my_array':[.......]}
Then you could write:
new JSONObject().getJSONArray("my_array");