• 6
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

Code in java class:

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;
    }

Code in Activity Class:

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

For the 1st time when I click on the button I'm not getting a response from the server but 2nd I'm getting a response can anyone help me out why not for the 1st time.

    • Another victim of asynchronous call . StringRequest is an asynchronous call and you just returned a null val cause its modifying inside onResponse.
      • 1
    • I see you are already using a callback like callBack.onSuccess();. So you do not need to return from method Just provide a callback method in both cases success and failure .
      • 1
    • if write the same code in activity class means I'm getting a response for the 1st time but why it is not working from java class file to activity class
      • 2
    • The reason its not working for first time is maybe you have declared val as global variable.

The solution is to use 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 
       }
  • 1
Reply Report
    • if write the same code in activity class means I'm getting a response for the 1st time but why it is not working from java class file to activity class
      • 1
    • Not getting response from the server on first-time click of a button but 2nd click on button im getting response so that is my issue
      • 1
    • This is because volley uses worker thread and the response takes time and before response is returned your method is returning string which is null.