private JSONArray jsonArray;
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//1、访问接口方法,返回系统账户名list为json数组
jsonArray= getJson("https://ip:port/account/list");
//2、遍历jsonArray数组内容
for(int i = 0; i < jsonArray.length(); i ++) {
try {
Log.e(TAG, "操作员列表:" + jsonArray.get(i).toString());
mList.add(jsonArray.get(i).toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
这两个步骤放在一个button点击事件,获取的jsonArray为空???
但是如果点击btn1 执行获取jsonArray接口事件,点击btn2 遍历jsonArray,就可以正常获取jsonArray数据。。这是啥原因??
public JSONArray getJson(String url){
OkGo.post(url_).tag("tag")
.execute(new ResponseCallback<String>(OperateInfoActivity.this) {
@Override
public void onSuccess(String o, Call call, Response response) {
super.onSuccess(o, call, response);
try {
Log.e("Login", "-- sucess " + o);
if (o != null) {
String responseStr = new String(o);
Log.e(TAG, "响应数据:" + responseStr);
JSONObject resultObject = new JSONObject(responseStr);
int errCode = resultObject.getInt("errCode");
String msg = resultObject.getString("msg");
message = msg;
Log.e(TAG, "message:" + message);
if (errCode == 200) {
String data = resultObject.getString("data");
JSONObject dataObject = new JSONObject(data);
jsonArray = (JSONArray) dataObject.get("operatorList");
} else {
}
}
} catch (Exception e) {
}
}
});
return jsonArray;
}
private void showMsg(final String msg) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(OperateInfoActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
}
}