一个奇怪的错误解析数据org.json.JSONException:输入结束时的字符0

时间:2022-10-29 14:28:55

I know it's an habitual question; but mine is really strange. I've done in another context exactly the same code where it works, but not on th

我知道这是一个习惯性的问题;但我真的很奇怪。我在另一个上下文中完成了相同的代码,但它没有

    String result = "";
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://192.168.1.145/test/getActivities.php");
    //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    InputStream is = entity.getContent();
    //convert response to string
    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();
    try{
        JSONArray jArray = new JSONArray(result);
        Log.e("log_tag", "lool");
        for(int i=0;i<jArray.length();i++){
            JSONObject json_data = jArray.getJSONObject(i);
            if (json_data.getString("friendly_url").toString().length() == 2){
                Sectors.add(new BasicNameValuePair(json_data.getString("title").toString(),json_data.getString("friendly_url").toString()));
            }
            else if(json_data.getString("friendly_url").toString().length() == 3){
                Branchs.add(new BasicNameValuePair(json_data.getString("title").toString(),json_data.getString("friendly_url").toString()));
            }
            else if(json_data.getString("friendly_url").toString().length() == 4){
                SBranches.add(new BasicNameValuePair(json_data.getString("title").toString(),json_data.getString("friendly_url").toString()));
            }
            else{
                SBranches.add(new BasicNameValuePair(json_data.getString("title").toString(),json_data.getString("friendly_url").toString()));
            }
            String[] lesSec = new String[Sectors.size()];
            for (int u = 0; u < Sectors.size(); u ++)
                lesSec[u] = jArray.getJSONObject(u).getString("title");
            setSectors(lesSec);
            Log.e("log_tag", lesSec[1]);

        }
    }catch(JSONException e){
        Log.e("log_tag", "Error parsing data "+e.toString());
    }

}catch(Exception e){
    Log.e("log_tag", "Error in http connection "+e.toString());
}

So this is the PHP file

所以这是PHP文件

<?php
$response = array();
$response["success"] = 0;
$response["message"] = "No products found";//*/


@mysql_connect("host","user","");
@mysql_select_db("db");

$q=@mysql_query("SELECT title, friendly_url FROM table");
while($e=@mysql_fetch_assoc($q))
        $OUTPUT[]=$e;

print(json_encode($OUTPUT));
print_r ($OUTPUT);


@mysql_close();
?>

Can anyone see the problem? I tried to print the $OUTPUT and this is the kind of result

有谁能看到这个问题?我试图打印$ OUTPUT,这就是结果

Array ( [0] => Array ( [title] => Activités de services autres et institutions 
                       [friendly_url] => 15 
                     ) 
        [1] => Array ( [title] => Batiment, travau ...etc

2 个解决方案

#1


0  

Try header('Content-Type: application/json'); before any output has been sent.

尝试标题('Content-Type:application / json');在任何输出发送之前。

As you show in your Answer, the result you got is actually not JSON!

当你在答案中显示时,你得到的结果实际上不是JSON!

See how JSON should look like:

了解JSON应如何:

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

source: http://json.org/example.html

来源:http://json.org/example.html

An array looks like: [ "one", "two", "three" ]

数组看起来像:[“one”,“two”,“three”]

Remove the print_r from your PHP script, it does not produce valid JSON format and succeeding text after a JSON string invalidates the output at all.

从PHP脚本中删除print_r,在JSON字符串使输出无效后,它不会生成有效的JSON格式和后续文本。

After correcting that issue you should also inspect, what the PHP functions mb_internal_encoding and mb_http_output return and compare it to the expected encoding in java.

在纠正该问题后,您还应该检查PHP函数mb_internal_encoding和mb_http_output返回什么,并将其与java中的预期编码进行比较。

#2


0  

Well; if anybody has the same problème with special characters, I've found the solution It's in the PHP; all what u have is encoding the result with special items like that :

好;如果有人和特殊字符有相同的问题,我找到了解决方案它在PHP中;所有你用的特殊项目编码结果:

while($e=@mysql_fetch_assoc($q)){
    $e['title'] = mb_convert_encoding($e['title'], 'UTF-8', 'pass');
    $OUTPUT[] = $e;
}

I don't know why, but it works !

我不知道为什么,但它的确有效!

#1


0  

Try header('Content-Type: application/json'); before any output has been sent.

尝试标题('Content-Type:application / json');在任何输出发送之前。

As you show in your Answer, the result you got is actually not JSON!

当你在答案中显示时,你得到的结果实际上不是JSON!

See how JSON should look like:

了解JSON应如何:

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

source: http://json.org/example.html

来源:http://json.org/example.html

An array looks like: [ "one", "two", "three" ]

数组看起来像:[“one”,“two”,“three”]

Remove the print_r from your PHP script, it does not produce valid JSON format and succeeding text after a JSON string invalidates the output at all.

从PHP脚本中删除print_r,在JSON字符串使输出无效后,它不会生成有效的JSON格式和后续文本。

After correcting that issue you should also inspect, what the PHP functions mb_internal_encoding and mb_http_output return and compare it to the expected encoding in java.

在纠正该问题后,您还应该检查PHP函数mb_internal_encoding和mb_http_output返回什么,并将其与java中的预期编码进行比较。

#2


0  

Well; if anybody has the same problème with special characters, I've found the solution It's in the PHP; all what u have is encoding the result with special items like that :

好;如果有人和特殊字符有相同的问题,我找到了解决方案它在PHP中;所有你用的特殊项目编码结果:

while($e=@mysql_fetch_assoc($q)){
    $e['title'] = mb_convert_encoding($e['title'], 'UTF-8', 'pass');
    $OUTPUT[] = $e;
}

I don't know why, but it works !

我不知道为什么,但它的确有效!