【问题标题】:JsonObjectrequest not getting executed after getting queuedJsonObjectrequest 在排队后没有被执行
【发布时间】:2016-03-16 01:27:21
【问题描述】:

我在我的片段的 onActivityCreated 中使用 JsonObject 请求。我正在将 jsonobject 请求添加到请求队列中。队列根本没有被执行。我已经在 Onresponse 和 Onerrorresponse 中加入了 Log 语句。就像请求根本没有开始一样。下面是我的代码。如果我做错了什么,请提出建议。我需要将一个字符串传递给服务器。所以我把它放在一个 jsonobject jlistname 中。服务器将返回一个 jsonobject,因此使用 jsonobjectrequest 而不是 stringrequest 接收它。

RequestQueue rq;
JsonObjectRequest jsObjRequest;
JSONObject jlistname = new JSONObject();
String listname="list1";

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    searchitembtn=(ImageButton) getView().findViewById(R.id.searchitembtn);
    rq = Volley.newRequestQueue(getActivity());
    try {
        jlistname.put("listname", listname);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    jsObjRequest = new JsonObjectRequest(Request.Method.POST, getlistitems, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.i(TAG, "im in onresponse" + response);
                    System.out.println(response);
                 }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.i(TAG, "im in onerrorresponse" + error);
                }
            }) {
        @Override
        public byte[] getBody() {
            return jlistname.toString().getBytes();
        }

        @Override
        public String getBodyContentType() {
            return "application/json";
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("Accept", "application/json");
            return headers;
        }
    };


    if (listtype==100||listtype==101){
        -----------------------
        }
    else if(listtype==102){
        Log.i(TAG,"mylisttypeis102: "+listtype);
        Log.i(TAG,"im before adding o request queue"+jsObjRequest);
        rq.add(jsObjRequest);
        Log.i(TAG, "im after adding o request queue"+rq);
    }
    else {
       -------------------
        }

    }
     showLists();

}

更新:我在布局中添加了一个按钮,并在按钮的 onclicklistener 中保留了 rq.add(jsObjRequest);

然后我得到正确的响应。

但我真的不希望点击响应。我希望根据我在 onActivityCreated 中的代码加载片段时生成响应。

有人可以解释为什么我只使用监听器得到响应吗?

【问题讨论】:

  • 请检查listtype的值
  • 嗨 @BNK 是的,列表类型是 102。else if 条件中的所有三个 Log 语句都将打印出来。
  • 还有其他可用的 logcat 信息吗?
  • 没有其他信息。登录onresponse,错误或异常,什么都没有。就好像,这个请求根本就没有被执行。
  • 啊,您没有将 URL 传递给请求。所以这就是原因:)

标签: android android-volley jsonobject jsonobjectrequest request-queueing


【解决方案1】:

将请求添加到 RequestQueue

rq.add(jsonObject);

【讨论】:

  • 我在 else if 部分做到了:else if(listtype==102){ Log.i(TAG,"mylisttypeis102: "+listtype); Log.i(TAG,"添加o请求队列前的im"+jsObjRequest); rq.add(jsObjRequest); Log.i(TAG, "添加o请求队列后的im"+rq); }
  • 检查 listvalue 输入
  • 我正在将请求添加到 elseif 部分的请求队列中。
  • 我知道了,listtype == 102 然后只会将它添加到请求队列中。否则它不会
  • 我在 else if 部分得到了所有三个日志语句。列表类型为 102,如果部分正在执行,则为 else。
    03-15 21:07:32.051 21206-21206/? I/swapna: mylisttypeis102: 102 03-15 21:07:32.051 21206-21206/? I/swapna: 在添加 o 请求队列 [] 192.168.1.7/getlistitems.php 0x355f91b NORMAL null 03-15 21:07:32.051 21206-21206/? I/swapna: im 后添加 o 请求 queuecom.android.volley.RequestQueue@2fb8c04c
猜你喜欢
  • 2016-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-11
  • 1970-01-01
  • 2014-04-22
  • 1970-01-01
相关资源
最近更新 更多