Android JSONArray长度值0
我试图获取我用于微调器的JSONArray长度值,但它总是返回0,即使微调器的值大于0。这是我的密码 类ConfigJSONAndroid JSONArray长度值0,android,arrays,json,Android,Arrays,Json,我试图获取我用于微调器的JSONArray长度值,但它总是返回0,即使微调器的值大于0。这是我的密码 类ConfigJSON public class ConfigJSON { public static int value; public static final String JSON_ARRAY = "result"; } 主要活动 protected void onCreate{ getData(); new UpdateUser.GetUser
public class ConfigJSON {
public static int value;
public static final String JSON_ARRAY = "result";
}
主要活动
protected void onCreate{
getData();
new UpdateUser.GetUserInfo(UpdateUser.this).execute();
}
public void getData() {
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject j = null;
try {
j = new JSONObject(response);
result = j.getJSONArray(ConfigJSON.JSON_ARRAY);
ConfigJSON.value = result.length();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private class GetUserInfo extends AsyncTask<Void, Void, Void> {
//Do Stuff before PostExecute
@Override
protected void onPostExecute(Void result) {
//The textview will output the JSONArray length
TextView msg = (TextView) findViewById(R.id.txtmsg);
msg.setText(Integer.toString(ConfigJSON.value));
int index;
for (int i=0;i<ConfigJSON.value;i++){
if (spinner.getItemAtPosition(i).toString().equals("stuff")){
index = i;
spinner.setSelection(index);
}
}
}
}
在onCreate中,并将其放入getData中
public void getData() {
//Same as the getData above but only this line at the end
new UpdateUser.GetUserInfo(UpdateUser.this).execute();
}
因为我想确保GetUserInfo将在设置getData的值后运行。但即使getData中有3个值,它仍然返回0。那么如何获取result.length()值并在GetUserInfo中使用它呢
我想确保GetUserInfo将在中的值之后运行
getData已设置
截击网络调用是异步的,因此在网络调用完成后执行任务,如下所示
public void getData() {
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject j = null;
try {
j = new JSONObject(response);
result = j.getJSONArray(ConfigJSON.JSON_ARRAY);
ConfigJSON.value = result.length();
new UpdateUser.GetUserInfo(UpdateUser.this).execute();
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
public void getData(){
StringRequest StringRequest=新的StringRequest(url,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
JSONObject j=null;
试一试{
j=新的JSONObject(响应);
结果=j.getJSONArray(ConfigJSON.JSON_数组);
ConfigJSON.value=result.length();
新建UpdateUser.GetUserInfo(UpdateUser.this.execute();
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}捕获(JSONException e){
e、 printStackTrace();
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
});
RequestQueue RequestQueue=Volley.newRequestQueue(this);
添加(stringRequest);
}
那么这里的string
是什么呢?这是一些东西,因此我可以与微调器值进行比较,如果它为真,则选择该微调器值。我会编辑它,这样人们就不会再问这个问题了。我很高兴我能帮上忙,很高兴编码
public void getData() {
//Same as the getData above but only this line at the end
new UpdateUser.GetUserInfo(UpdateUser.this).execute();
}
public void getData() {
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject j = null;
try {
j = new JSONObject(response);
result = j.getJSONArray(ConfigJSON.JSON_ARRAY);
ConfigJSON.value = result.length();
new UpdateUser.GetUserInfo(UpdateUser.this).execute();
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}