简体   繁体   English

Android JSON解析凌空库?

[英]Android JSON Parsing volley library?

I have been struggling to understand the logic of the Android JSON Parsing with volley libraries.I am trying to get JSON Array and parse with Volley Libraries.I understand how JSON data is extracted from PHP file but I have big problems. 我一直在努力理解使用Volley库进行Android JSON解析的逻辑。我试图获取JSON Array并使用Volley Libraries进行解析。我了解如何从PHP文件中提取JSON数据,但是我遇到了很多问题。 makeJsonArrayRequest() function runs correctly and parse JSON Array from get_data.php file and add users ArrayList from User class in each iteration.I called this function in onCreate and userLogin function individually.Size of users ArrayList equals to 0 when I call makeJsonArrayRequest() function in onCreate method .However,Size of users ArrayList equals to nonzero number when I call makeJsonArrayRequest() function in userLogin method(This method called when clicked on Login Button).This is my problem.Why makeJsonArrayRequest() doesn't run on Create() method ? makeJsonArrayRequest()函数正确运行并从get_data.php文件中解析JSON Array,并在每次迭代中从User类添加用户ArrayList。我分别在onCreate和userLogin函数中调用此函数。当我调用makeJsonArrayRequest()时,用户ArrayList的大小等于0 onCreate方法中的函数。但是,当我在userLogin方法中调用makeJsonArrayRequest()函数(单击登录按钮时调用此方法)时,用户ArrayList的大小等于非零数。这是我的问题。为什么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);
}

If what you're saying is : 如果您要说的是:

I run the request in onCreate() and check - it's empty. 我在onCreate()中运行请求并检查-它为空。

and then: 接着:

I run it again LATER, and Check - it's full. 我稍后再次运行它,然后检查-它已满。

then have you considered the request latency? 那么您是否考虑过请求延迟? it takes time (albeit - not much time) for the request to return, you might be checking before it does 请求返回需要时间(尽管-时间不多),您可能需要先检查一下

EDIT: Run what you want inside the onResponse(), that's only called when the request returns. 编辑:在onResponse()中运行所需的内容,只有在请求返回时才调用它。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM