Java json解析期间输入结束,字符0处出错

Java json解析期间输入结束,字符0处出错,java,android,Java,Android,我是android新手,所以当我尝试使用以下代码解析从mysql数据库获取的json时,会发生什么情况: public ParseJSON(String json){ this.json = json; } protected void parseJSON(){ JSONObject jsonObject=null; try { jsonObject = new JSONObject(json);

我是android新手,所以当我尝试使用以下代码解析从mysql数据库获取的json时,会发生什么情况:

public ParseJSON(String json){
        this.json = json;
    }

    protected void parseJSON(){
        JSONObject jsonObject=null;
        try {
            jsonObject = new JSONObject(json);




            users = jsonObject.getJSONArray(JSON_ARRAY);

            //ids = new String[users.length()];
            names = new String[users.length()];
            message = new String[users.length()];

            for(int i=0;i<users.length();i++){
                JSONObject jo = users.getJSONObject(i);
               // ids[i] = jo.getString(KEY_ID);
                names[i] = jo.getString(KEY_NAME);
                message[i] = jo.getString(KEY_EMAIL);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
公共解析json(字符串json){ this.json=json; } 受保护的void parseJSON(){ JSONObject JSONObject=null; 试一试{ jsonObject=新的jsonObject(json); users=jsonObject.getJSONArray(JSON_数组); //ids=新字符串[users.length()]; 名称=新字符串[users.length()]; message=新字符串[users.length()];
对于(inti=0;i,在我看来,您实际上是在以不同的方式定义事物,如果您正在进行解析,那么请将每个jsonObject或JsonArray作为本地对象。 请参见我向您提供的代码:

try {
           JsonObject jsonObject = new JSONObject(json);

           JsonArray users = jsonObject.getJSONArray(JSON_ARRAY);

            for(int i=0;i<users.length();i++){
                JSONObject jo = users.getJSONObject(i);
                String name = jo.getString(KEY_NAME);
                String message = jo.getString(KEY_EMAIL);


            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
试试看{
JsonObject JsonObject=新的JsonObject(json);
JsonArray users=jsonObject.getJSONArray(JSON_数组);

对于(int i=0;i我得到了相同的错误,并通过在android internet清单中授予权限修复了它。在从网络解析json时,请始终记住将此代码添加到清单中

<uses-permission android:name="android.permission.INTERNET" />

<application
    ...>



在解析之前,是否有任何方法打印出响应?可能是响应为空。我添加了一个代码,在浏览器中键入url时从中提取数据()没有显示任何内容,这是一个空白页面。因此,正如我在第一条评论中指出的,响应可能是空白的。我正在使用Postman从数据库获取响应,但是我在那里获取了值,但是在浏览器中,在读取
users=jsonObject.getJSONArray(JSON_数组)的行之前,不是真的
,放置一行内容为
System.out.println(JSON_数组);
,然后您的问题将显示此消息。正如@LearningPhase所述,对我来说,php脚本的输出长度为0;这与您在Java中看到的异常一致。我认为错误发生在服务器端。
try {
           JsonObject jsonObject = new JSONObject(json);

           JsonArray users = jsonObject.getJSONArray(JSON_ARRAY);

            for(int i=0;i<users.length();i++){
                JSONObject jo = users.getJSONObject(i);
                String name = jo.getString(KEY_NAME);
                String message = jo.getString(KEY_EMAIL);


            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
<uses-permission android:name="android.permission.INTERNET" />

<application
    ...>