dongyi1524 2015-11-12 18:56
浏览 85
已采纳

Android JSON解析凌空图书馆?

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. 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 ?

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);
}
  • 写回答

1条回答 默认 最新

  • doutucui0133 2015-11-28 20:12
    关注

    If what you're saying is :

    I run the request in onCreate() and check - it's empty.

    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.

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?