【问题标题】:Error parsing data [Value p of type java.lang.String cannot be converted to JSONObject解析数据时出错 [java.lang.String 类型的值 p 无法转换为 JSONObject
【发布时间】:2014-02-04 18:13:01
【问题描述】:

我正在关注如何将 android APP 连接到 mysql 数据库的教程。 该应用程序有 2 个功能: 显示产品(不起作用) 创建产品(作品) 当我制作产品时,它成功注册到数据库,但我的 JSOn 解析器出现了一些错误。

我发现很多帖子都在处理同样的问题,但我仍然遇到问题。

E/JSON Parser(1216):解析数据时出错 [java.lang.String 类型的值 p 无法转换为 JSONObject] get_all_products.php

这是我的解析器

public class JSONParser {


    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }


    // function get json from url
    // by making HTTP POST or GET mehtod
    public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        // Making HTTP request
        try {

            // check for request method
            if(method == "POST"){
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            }else if(method == "GET"){
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }           

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        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();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }


        try {
            jObj = new JSONObject(json.substring(19));
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data [" + e.getMessage()+"] "+json);
        }

        // return JSON String
        return jObj;

    }

}

【问题讨论】:

  • 发布相关的json。看起来响应是一个字符串
  • 你为什么在 JSON String 上调用 substring?
  • 只是一个注释。 JSON 编码应为 UTF-8(tools.ietf.org/html/rfc4627,第 3 秒)。
  • 谢谢大家的回答,这段代码的原文是:jObj = new JSONObject(json); } catch (JSONException e) { Log.e("JSON Parser", "错误解析数据" + e.toString());我从论坛上拿了它的子字符串...
  • 好吧,伙计们,我的问题已解决,我的 php 文件有问题...我在代码顶部(在 之前)放了一些文本。谢谢!

标签: java android mysql json


【解决方案1】:

我遇到了同样的问题,我发现是因为我留下了p 在 PHP 文件开始之前。

【讨论】:

    猜你喜欢
    • 2015-12-25
    • 2013-09-07
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多