繁体   English   中英

永远不会执行Android JsonArrayRequest onResponse

[英]Android JsonArrayRequest onResponse is never executed

这个问题整日困扰着我。 我已经使用调试器逐步完成了该程序,但它从未进入Response.Listener。 它也不会进入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);

}

任何帮助将不胜感激。

编辑:我从整个onCreate方法中添加了更多代码,以提供更多帮助信息。

您已经创建了JsonArrayRequest类的historyReq对象;

现在,在JsonArrayRequest类内部应该有一些方法调用,该方法利用了您在historyReq内部注入的Response.ErrorListener()对象。 在historyReq内部注入的对象应使用onErrorResponse方法。

有关更多信息,您可以看到Anonymous类的行为。

暂无
暂无

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

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