【问题标题】:Error parsing data org.json.JSONException: Value db_connect.php of type java.lang.String cannot be converted to JSONObject解析数据 org.json.JSONException 时出错:java.lang.String 类型的值 db_connect.php 无法转换为 JSONObject
【发布时间】:2013-10-27 22:20:49
【问题描述】:

我不知道我的应用出了什么问题。目前,我正在开发一个餐厅查找器应用程序。对于数据库,我使用 PHP 和 mySQL。我从界面插入的数据出现在数据库(localhost)中。但是,该应用程序一直显示消息“应用程序 RestFinder (process.com.example.restaurantfinder) 已意外停止。请重试。”然后它崩溃了。

当我查看日志文件时,第一个日志文件是:

Error parsing data org.json.JSONException: Value db_connect.php of type java.lang.String cannot be converted to JSONObject

谁能给我一个解决方案?

这些是我怀疑导致错误的 JSONParser.java 的代码。

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 parse the string to a JSON object
    try {
        jObj = new JSONObject(json.substring(6));
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;
}

【问题讨论】:

  • 请告诉我们导致错误的代码。
  • ...以及输入。
  • Matt Ball:你是说快照吗?

标签: java php android json eclipse


【解决方案1】:

您能否发布导致此异常的 JSON 数据输入。

您也可以从 stockoverflow 参考这个解决方案 - JSONException: Value of type java.lang.String cannot be converted to JSONObject

【讨论】:

    【解决方案2】:

    db_connect.php 类型为 java.lang.String

    这表明您不是做错事的人(这次 :D),服务器配置错误,您得到的响应无效。

    请发布指定 URL 的输出以获得此结果,以及您正在运行的 php 脚本。

    【讨论】:

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