第一次没有收到来自服务器截击的响应

时间:2019-03-07 05:27:50

标签: android android-volley

java类中的代码:

public String POST(String url, final Map<String, String> params, final VolleyCallBack callBack) {

        //RequestFuture<String> requestFuture = new RequestFuture.newFuture();
        RequestFuture<String> future = RequestFuture.newFuture();
        progressDialog = new ProgressDialog(context);
        progressDialog.setMessage("Loading....");
        progressDialog.show();
        StringRequest stringRequest = new StringRequest(
                Request.Method.POST,
                url,

                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            progressDialog.dismiss();

                            val = jsonObject.getString("message");
                            callBack.onSuccess();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                progressDialog.dismiss();
                ToastMsg(error.getMessage() + " " + error.getStackTrace()[0]);
            }
        }
        ) {
            @Override
            protected Map<String, String> getParams() {
                return params;
            }
        };

        RetryPolicy mRetryPolicy = new DefaultRetryPolicy(
                0,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        stringRequest.setRetryPolicy(mRetryPolicy);

        RequestQueue requestQueue = Volley.newRequestQueue(context);
        stringRequest.setShouldCache(false);
        requestQueue.add(stringRequest);


        return val;
    }

活动类别中的代码

val = db.POST(Constants.URL_FacultyLogin, map);

第一次单击按钮时,没有收到服务器的响应,但是第二次收到响应,有人可以第一次为什么不帮我。

1 个答案:

答案 0 :(得分:1)

解决方案是使用asynctask Refer this

 @Override
        protected String doInBackground(String... params) {
            //Call your volley function here
        }



@Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
        //The result variable holds your val returned from volley 
       }