简体   繁体   English

永远不会执行Android JsonArrayRequest onResponse

[英]Android JsonArrayRequest onResponse is never executed

this issue has been bugging me all day. 这个问题整日困扰着我。 I have stepped through the program with the debugger and it never goes into the Response.Listener. 我已经使用调试器逐步完成了该程序,但它从未进入Response.Listener。 It doesn't go into onErrorResponse either so the API isn't throwing an error. 它也不会进入onErrorResponse,因此API不会引发错误。

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_history_rewards);

    prf = new PrefManager(this);

    getSupportActionBar().setIcon(R.drawable.ic_back_icon);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setTitle(R.string.earning_history);


    listView = (ListView) findViewById(R.id.list);
    TextView emptyText = (TextView) findViewById(R.id.empty);
    emptyText.setText(getString(R.string.no_rewards_yet));
    adapter = new UserHistoryAdapter(EarningHistoryActivity.this, historyList);
    listView.setAdapter(adapter);
    listView.setEmptyView(emptyText);
    listView.setDivider(null);

    pDialog = new ProgressDialog(this);
    pDialog.setMessage(getString(R.string.loading));
    pDialog.show();

    // changing action bar color
    // getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#1b1b1b")));

    JsonArrayRequest historyReq = new JsonArrayRequest(Config.Base_Url+"api/earning_history.php?username="+App.getInstance().getUsername(), new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                    Log.d(TAG, response.toString());
                    hidePDialog();

                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {
                            JSONObject obj = response.getJSONObject(i);
                            UserHistory history = new UserHistory();
                            history.setTitle(obj.getString("type"));
                            history.setRating(obj.getString("date"));
                            history.setThumbnailUrl(Config.Base_Url+"images/reward.png");
                            history.setYear(obj.getString("points"));
                            //history.setGenre(obj.getString("time"));

                            historyList.add(history);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            hidePDialog();

        }
    });

    // Adding request to request queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(historyReq);

}

Any help would be greatly appreciated. 任何帮助将不胜感激。

Edit: I added more code from the entire onCreate method to give some more information to help. 编辑:我从整个onCreate方法中添加了更多代码,以提供更多帮助信息。

You have created historyReq object of class JsonArrayRequest; 您已经创建了JsonArrayRequest类的historyReq对象;

Now there should be some method call inside JsonArrayRequest class which make utilize the Response.ErrorListener() object that you injected inside the historyReq. 现在,在JsonArrayRequest类内部应该有一些方法调用,该方法利用了您在historyReq内部注入的Response.ErrorListener()对象。 The injected objected inside historyReq should make use of on onErrorResponse method. 在historyReq内部注入的对象应使用onErrorResponse方法。

For more info you can see behavior of Anonymous class. 有关更多信息,您可以看到Anonymous类的行为。

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

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