【问题标题】:Error parsing dataorg.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject解析 dataorg.json.JSONException 时出错:Java.lang.String 类型的值 <br 无法转换为 JSONObject
【发布时间】:2015-09-19 20:02:40
【问题描述】:

我正在尝试为我的论文制作 android 食品订单,因为这个错误我没时间了 :(

logcat 错误:

解析 dataorg.json.JSONException 时出错:无法将值转换为 JSONObject org.json.JSONException: JSONObject 的值

这是我的 JSONParser:

package com.makanan.restotradisional;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

public JSONParser() {
}

// fungsi abil json url lewat method HTTP POST atau GET
public JSONObject makeHttpRequest(String url, String method,
        List<NameValuePair> params) {
    try {
        if (method == "POST") {
            // jika request method adalah 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") {
            // jika request method adalah 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);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data" + e.toString());
        e.printStackTrace();
    }
    return jObj;

}
}

这是我的 PHP 和 Java:http://www.4shared.com/rar/1lGplX19ba/Java_and_PHP.html

这是我在 phpmyadmin 上的数据库:http://www.4shared.com/rar/y_UMtL7_ce/rumah_makan.html

请帮帮我

【问题讨论】:

  • 请提供您在JSONObject构造函数中传递的String

标签: java php android mysql json


【解决方案1】:

从您的 php 文件中删除任何 &lt;br&gt; 语句或 echo 语句,除了用于传递 json 的语句。

在浏览器中检查文件的输出,删除除 json 之外的所有不需要的东西..

【讨论】:

    【解决方案2】:

    请打印并检查您的字符串json 的格式是否符合JSONObject 构造函数的预期。根据文档,构造 JSONObject 的有效 json 字符串应该是 -- {(左大括号)开头并以 }(右大括号)结尾的字符串。

    请参考this

    【讨论】:

      【解决方案3】:

      进入 JSONParser 并执行此操作,以便您在 logcat 中看到来自 php 的内容。可能是 php 错误。

      try {
              jObj = new JSONObject(json);
          } catch (JSONException e) {
              //This line is what u need to add
              Log.d("Whats wrong?", json.toString());
              Log.e("JSON Parser", "Error parsing data " + e.toString());
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-02
        • 1970-01-01
        • 2023-03-10
        • 2020-04-02
        • 2017-08-12
        • 1970-01-01
        相关资源
        最近更新 更多