0

我将直接进入它。我在 LogCat 中收到一个错误,说我的 JSON 字符串无法转换为 JSONOject,我真的不知道如何解决这个错误,所以我只是希望比我有更好知识的人能给我关于如何我的建议可以关注这个问题

日志猫:

08-02 00:12:28.485: E/JSON Parser(778): Error parsing data org.json.JSONException: Value <!doctype of type java.lang.String cannot be converted to JSONObject

JSONParser 类

 public class JSONParser {

    InputStream is;
    JSONObject jObj;
    String json = "";
    public EditText et;

    public JSONParser () {
    }

    public JSONObject readJSONFeed(String URL) {

        try{
        HttpClient client = new DefaultHttpClient();
        HttpPost request = new HttpPost(URL);
        //request.setURI(website);
        try {
            HttpResponse response = client.execute(request);
        HttpEntity httpEntity = response.getEntity();
        is = httpEntity.getContent();

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line;
            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());
        }

        return jObj;

        }finally{}

    }{
    }}

这是导致出现此错误的方法:

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

我打印了我的 JSON 字符串:

08-02 20:02:27.535: D/JSON String(795): <!doctype html public "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
08-02 20:02:27.535: D/JSON String(795): <html><head><title>Yahoo! - 404 Not Found</title><style>
08-02 20:02:27.535: D/JSON String(795): /* nn4 hide */ 
08-02 20:02:27.535: D/JSON String(795): /*/*/
08-02 20:02:27.535: D/JSON String(795): body {font:small/1.2em arial,helvetica,clean,sans-serif;font:x-small;text-align:center;}table {font-size:inherit;font:x-small;}
08-02 20:02:27.535: D/JSON String(795): html>body {font:83%/1.2em arial,helvetica,clean,sans-serif;}input {font-size:100%;vertical-align:middle;}p, form {margin:0;padding:0;}
08-02 20:02:27.535: D/JSON String(795): p {padding-bottom:6px;margin-bottom:10px;}#doc {width:48.5em;margin:0 auto;border:1px solid #fff;text-align:center;}#ygma {text-align:right;margin-bottom:53px}
08-02 20:02:27.535: D/JSON String(795): #ygma img {float:left;}#ygma div {border-bottom:1px solid #ccc;padding-bottom:8px;margin-left:152px;}#bd {clear:both;text-align:left;width:75%;margin:0 auto 20px;}
08-02 20:02:27.535: D/JSON String(795): h1 {font-size:135%;text-align:center;margin:0 0 15px;}legend {display:none;}fieldset {border:0 solid #fff;padding:.8em 0 .8em 4.5em;}
08-02 20:02:27.535: D/JSON String(795): form {position:relative;background:#eee;margin-bottom:15px;border:1px solid #ccc;border-width:1px 0;}
08-02 20:02:27.535: D/JSON String(795): #s1p {width:15em;margin-right:.1em;}
08-02 20:02:27.535: D/JSON String(795): form span {position:absolute;left:70%;top:.8em;}form a {font:78%/1.2em arial;display:block;padding-left:.8em;white-space:nowrap;background: url(http://l.yimg.com/a/i/s/bullet.gif) no-repeat left center;} 
08-02 20:02:27.535: D/JSON String(795): form .sep {display:none;}.more {text-align:center;}#ft {padding-top:10px;border-top:1px solid #999;}#ft p {text-align:center;font:78% arial;}
08-02 20:02:27.535: D/JSON String(795): /* end nn4 hide */
08-02 20:02:27.535: D/JSON String(795): </style></head>
08-02 20:02:27.535: D/JSON String(795): <body><div id="doc">
08-02 20:02:27.535: D/JSON String(795): <div id="ygma"><a href="http://us.rd.yahoo.com/404/*http://www.yahoo.com"><img
08-02 20:02:27.535: D/JSON String(795): src=http://l.yimg.com/a/i/yahoo.gif
08-02 20:02:27.535: D/JSON String(795): width=147 height=31 border=0 alt="Yahoo!"></a><div><a
08-02 20:02:27.535: D/JSON String(795): href="http://us.rd.yahoo.com/404/*http://www.yahoo.com">Yahoo!</a>
08-02 20:02:27.535: D/JSON String(795):  - <a href="http://us.rd.yahoo.com/404/*http://help.yahoo.com">Help</a></div></div>
08-02 20:02:27.535: D/JSON String(795): <div id="bd"><h1>Sorry, the page you requested was not found.</h1>
08-02 20:02:27.535: D/JSON String(795): <p>Please check the URL for proper spelling and capitalization. If
08-02 20:02:27.535: D/JSON String(795): you're having trouble locating a destination on Yahoo!, try visiting the
08-02 20:02:27.535: D/JSON String(795): <strong><a
08-02 20:02:27.535: D/JSON String(795): href="http://us.rd.yahoo.com/404/*http://www.yahoo.com">Yahoo! home
08-02 20:02:27.535: D/JSON String(795): page</a></strong> or look through a list of <strong><a
08-02 20:02:27.535: D/JSON String(795): href="http://us.rd.yahoo.com/404/*http://docs.yahoo.com/docs/family/more/">Yahoo!'s
08-02 20:02:27.535: D/JSON String(795): online services</a></strong>. Also, you may find what you're looking for
08-02 20:02:27.535: D/JSON String(795): if you try searching below.</p>
08-02 20:02:27.535: D/JSON String(795): <form name="s1" action="http://us.rd.yahoo.com/404/*-http://search.yahoo.com/search"><fieldset>
08-02 20:02:27.535: D/JSON String(795): <legend><label for="s1p">Search the Web</label></legend>
08-02 20:02:27.535: D/JSON String(795): <input type="text" size=30 name="p" id="s1p" title="enter search terms here">
08-02 20:02:27.535: D/JSON String(795): <input type="submit" value="Search">
08-02 20:02:27.535: D/JSON String(795): <span><a href="http://us.rd.yahoo.com/404/*http://search.yahoo.com/search/options?p=">advanced search</a> <span class=sep>|</span> <a href="http://us.rd.yahoo.com/404/*http://buzz.yahoo.com">most popular</a></span>
08-02 20:02:27.535: D/JSON String(795): </fieldset></form>
08-02 20:02:27.535: D/JSON String(795): <p class="more">Please try <strong><a
08-02 20:02:27.535: D/JSON String(795): href="http://us.rd.yahoo.com/404/*http://help.yahoo.com">Yahoo!
08-02 20:02:27.535: D/JSON String(795): Help Central</a></strong> if you need more assistance.</p>
08-02 20:02:27.535: D/JSON String(795): </div><div id="ft"><p>Copyright &copy; 2013 Yahoo! Inc.
08-02 20:02:27.535: D/JSON String(795): All rights reserved. <a
08-02 20:02:27.535: D/JSON String(795): href="http://us.rd.yahoo.com/404/*http://privacy.yahoo.com">Privacy
08-02 20:02:27.535: D/JSON String(795): Policy</a> - <a
08-02 20:02:27.535: D/JSON String(795): href="http://us.rd.yahoo.com/404/*http://docs.yahoo.com/info/terms/">Terms
08-02 20:02:27.535: D/JSON String(795): of Service</a></p></div>
08-02 20:02:27.535: D/JSON String(795): </div></body></html>
4

2 回答 2

0

你的字符串必须是 JSON 编码的。 请参阅您尝试使用的构造函数的 android 文档。 这意味着您的字符串应如下所示:

{ "key1": "value1", "key2": "value2", "key3": "value3" } 

可能带有转义引号。

根据您的错误消息,您似乎正在尝试从 HTML 或其他一些标记语言创建 JSONObject。您可能想要验证您正在调用的 URL 是否实际返回了有效的 JSON,并且您正在添加所有正确的参数和标头以防止收到错误页面作为响应。

于 2013-08-02T16:09:20.817 回答
0

试试dis希望它对你有用

 public class TestObjectToJson {
      public int k1 = 1;
      public String k2 = "Test";

      public static void main(String[] args) {
          TestObjectToJson obj = new TestObjectToJson();
          Gson gson = new Gson();
    String json = gson.toJson(obj);

          System.out.println(json);
      }

    }

作为参考,你可以看到这个

https://code.google.com/p/google-gson/
于 2013-08-02T16:22:42.640 回答