通过PHP连接MySQL,JSON错误

发布于 2024-12-19 07:13:48 字数 1477 浏览 0 评论 0原文

我编写了一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捂风挽笑 2024-12-26 07:13:48

听起来您的 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");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文