Android 索引0超出范围[0…0]
我在Json方法中得到了这一点。 我试图从我的服务器和mysql数据库中获取信息,然后将其显示在我的用户名文本视图中。 这是我的Json方法。它抛出索引超出范围错误Android 索引0超出范围[0…0],android,mysql,json,mysqli,Android,Mysql,Json,Mysqli,我在Json方法中得到了这一点。 我试图从我的服务器和mysql数据库中获取信息,然后将其显示在我的用户名文本视图中。 这是我的Json方法。它抛出索引超出范围错误 private void showJSON(String response) { String username = ""; try { JSONObject jsonObject = new JSONObject(response); JSONArray result = jso
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();
}
数组的长度可能为0。请检查。您可以尝试验证中的响应变量 私有void showJSONString响应{
不为空使用Gson解码json将比您使用的方式方便得多,而且显然JSONArray结果的长度为0这都是Java代码。mysqli的作用在哪里?我使用php和mysqli检索信息。