【问题标题】:Index 0 out of range [0...0]索引 0 超出范围 [0...0]
【发布时间】:2016-12-16 06:06:56
【问题描述】:

我在我的 Json 方法中得到了这个。 我正在尝试从我的服务器和 mysql 数据库中获取信息,然后将其显示在我的用户名文本视图中。 这是我的 Json 方法。它会抛出索引超出范围错误。

 private void showJSON(String response) {
    String username = "";

    try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(Constantss.JSON_ARRAY);
        JSONObject profileData = result.getJSONObject(0);
        username = profileData.getString(Constantss.USERNAME);

        System.out.println("first" + username);


    } catch (JSONException e) {
        e.printStackTrace();

    }

    usernameView.setText(username);
    System.out.println("second" + username);

}

可能不需要,但这就是我获取数据的方式,它工作正常我能够检索 Id 并运行 php 代码给我用户名。

 private void getData() {
    class lata extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                    getApplicationContext(), //Context
                    "us-east--------------",   //Identity Pool ID
                    Regions.US_EAST_1 // Region
                    //Now we retrieve
            );

//        return null;
            String identityID = credentialsProvider.getIdentityId();
            Log.d("LogTag", "my ID is" + identityID);
            String id = identityID;
            String userid = identityID;

            System.out.println("Id is" + identityID);
            System.out.println("Id tis" + id);
            String url = Constantss.PROFILE_URL + identityID;
            StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    showJSON(response);
                }
            },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(Profile.this, error.getMessage().toString(), Toast.LENGTH_SHORT).show();

                        }
                    });
            RequestQueue requestQueue =     Volley.newRequestQueue(Profile.this);
            requestQueue.add(stringRequest);


            return identityID;
        }


    }
    lata pasta = new lata();
    pasta.execute();
}

【问题讨论】:

  • 这都是Java代码。 mysqli 在哪里考虑?
  • 我正在使用php to和Mysqli来检索信息。

标签: android mysql json mysqli


【解决方案1】:

你的数组的长度可能是0,请检查一下。

【讨论】:

    【解决方案2】:

    您可以尝试在中验证响应变量

    private void showJSON(String response) {

    不为空

    【讨论】:

      【解决方案3】:

      使用Gson解码json会比你的方式方便很多,而且很明显JSONArray结果的长度是0

      【讨论】:

        猜你喜欢
        • 2012-04-07
        • 2019-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多