Android JSON 解析排球库?
Android JSON Parsing volley library?
我一直在努力理解 Android JSON 使用 volley 进行解析的逻辑 libraries.I 我正在尝试获取 JSON 数组并使用 Volley [=16] 进行解析=] 了解如何从 PHP 文件中提取 JSON 数据,但我遇到了大问题。 makeJsonArrayRequest() 函数 运行s 正确解析 JSON 文件中的 get_data.php 数组,并在每个 iteration.I 中从用户 class 添加用户 ArrayList 在 onCreate 中调用此函数并当我在 onCreate 方法中调用 makeJsonArrayRequest() 函数时,用户 ArrayList 的 userLogin 函数 individually.Size 等于 0。但是,当我在 userLogin 方法中调用 makeJsonArrayRequest() 函数时,用户 ArrayList 的大小等于非零数(单击时调用此方法在登录按钮上)。这是我的 problem.Why makeJsonArrayRequest() 不 运行 在 Create() 方法上 ?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ET_NAME = (EditText) findViewById(R.id.user_name);
ET_PASS = (EditText) findViewById(R.id.user_pass);
pDialog = new ProgressDialog(this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
makeJsonArrayRequest();
Toast.makeText(getApplicationContext(),"Size:" + users.size(),Toast.LENGTH_LONG).show();
}
public void userLogin(View view) {
login_name = ET_NAME.getText().toString();
login_pass = ET_PASS.getText().toString();
String method = "login";
String status = "1";
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.execute(method, login_name, login_pass, status);
Intent i = new Intent(this,MapsActivity.class);
i.putExtra("username",login_name);
i.putExtra("userpass", login_pass);
makeJsonArrayRequest();
startActivity(i);
Toast.makeText(getApplicationContext(),"Size:" + users.size(),Toast.LENGTH_LONG).show();
}
private void makeJsonArrayRequest() {
showpDialog();
JsonArrayRequest req = new JsonArrayRequest(JSON_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
try {
// Parsing json array response
// loop through each json object
jsonResponse ="";
for (int i = 0; i < response.length(); i++) {
JSONObject person = (JSONObject) response.get(i);
String name = person.getString("name");
String username = person.getString("username");
String password = person.getString("password");
double latitude = Double.parseDouble(person.getString("latitude"));
double longitude = Double.parseDouble(person.getString("longitude"));
String status = person.getString("status");
User user = new User(name,username,password,latitude,longitude,status);
users.add(user);
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
hidepDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(req);
}
如果你说的是:
I run the request in onCreate() and check - it's empty.
然后:
I run it again LATER, and Check - it's full.
那你考虑过请求延迟吗?请求 return 需要时间(虽然 - 时间不多),你可能会在它
之前检查
编辑:
运行 onResponse() 中你想要的,只有在请求 returns 时才会调用。
希望对您有所帮助。
我一直在努力理解 Android JSON 使用 volley 进行解析的逻辑 libraries.I 我正在尝试获取 JSON 数组并使用 Volley [=16] 进行解析=] 了解如何从 PHP 文件中提取 JSON 数据,但我遇到了大问题。 makeJsonArrayRequest() 函数 运行s 正确解析 JSON 文件中的 get_data.php 数组,并在每个 iteration.I 中从用户 class 添加用户 ArrayList 在 onCreate 中调用此函数并当我在 onCreate 方法中调用 makeJsonArrayRequest() 函数时,用户 ArrayList 的 userLogin 函数 individually.Size 等于 0。但是,当我在 userLogin 方法中调用 makeJsonArrayRequest() 函数时,用户 ArrayList 的大小等于非零数(单击时调用此方法在登录按钮上)。这是我的 problem.Why makeJsonArrayRequest() 不 运行 在 Create() 方法上 ?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ET_NAME = (EditText) findViewById(R.id.user_name);
ET_PASS = (EditText) findViewById(R.id.user_pass);
pDialog = new ProgressDialog(this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
makeJsonArrayRequest();
Toast.makeText(getApplicationContext(),"Size:" + users.size(),Toast.LENGTH_LONG).show();
}
public void userLogin(View view) {
login_name = ET_NAME.getText().toString();
login_pass = ET_PASS.getText().toString();
String method = "login";
String status = "1";
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.execute(method, login_name, login_pass, status);
Intent i = new Intent(this,MapsActivity.class);
i.putExtra("username",login_name);
i.putExtra("userpass", login_pass);
makeJsonArrayRequest();
startActivity(i);
Toast.makeText(getApplicationContext(),"Size:" + users.size(),Toast.LENGTH_LONG).show();
}
private void makeJsonArrayRequest() {
showpDialog();
JsonArrayRequest req = new JsonArrayRequest(JSON_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
try {
// Parsing json array response
// loop through each json object
jsonResponse ="";
for (int i = 0; i < response.length(); i++) {
JSONObject person = (JSONObject) response.get(i);
String name = person.getString("name");
String username = person.getString("username");
String password = person.getString("password");
double latitude = Double.parseDouble(person.getString("latitude"));
double longitude = Double.parseDouble(person.getString("longitude"));
String status = person.getString("status");
User user = new User(name,username,password,latitude,longitude,status);
users.add(user);
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
hidepDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(req);
}
如果你说的是:
I run the request in onCreate() and check - it's empty.
然后:
I run it again LATER, and Check - it's full.
那你考虑过请求延迟吗?请求 return 需要时间(虽然 - 时间不多),你可能会在它
之前检查编辑: 运行 onResponse() 中你想要的,只有在请求 returns 时才会调用。
希望对您有所帮助。