【发布时间】: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来检索信息。