0

I've created this code to access user from my database for Login purpose. I can access the object 'st' when I'm inside OnResponse method but when I try to return return the object, it gives me null. Also when I try to access this st object before returning, it gives NullPointerException. What is the exact problem?

 public class ServerRequests {

    ProgressDialog progressDialog;
    public static user_Student st;
    public static final int CONNECTION_TIMEOUT = 1000 * 15;
    public static final String SERVER_ADDRESS = "http://prem-pc:8989/";
    Context ct;

    public ServerRequests(Context context) {
        ct = context;
        progressDialog = new ProgressDialog(context);
        progressDialog.setCancelable(false);
        progressDialog.setTitle("Processing");
        progressDialog.setMessage("Please Wait....");

    }

    public ServerRequests() {

    }

    public user_Student fetchUserDataInBackground(user_Student user) {
        progressDialog.show();
        Toast.makeText(ct, "Data in background: ", Toast.LENGTH_SHORT).show();
        user_Student ust = doInBackground(user);
        progressDialog.dismiss();
        return ust;
    }

    public user_Student doInBackground(user_Student user) {

        String URL = SERVER_ADDRESS + "connect.php?prn=" + user.prn + "&password=" + user.password;

        RequestQueue req = Volley.newRequestQueue(ct);
        Toast.makeText(ct, "Do in Background", Toast.LENGTH_SHORT).show();
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, URL, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jObject) {

                try {
                    // Parsing json object response
                    // response will be a json object

                    if (jObject.length() == 0) {
                        st = null;
                        Toast.makeText(ct, "Null JSON Object", Toast.LENGTH_SHORT).show();
                    } else {

                        String prn = jObject.getString("prn");
                        String fname = jObject.getString("fname");
                        String mname = jObject.getString("mname");
                        String lname = jObject.getString("lname");
                        String clas = jObject.getString("clas");
                        String dept = jObject.getString("dept");
                        String batch = jObject.getString("batch");
                        String scontact = jObject.getString("scontact");
                        String pcontact = jObject.getString("pcontact");
                        String email = jObject.getString("email");
                        String password = jObject.getString("password");
                        String dob = jObject.getString("dob");

                        st = new user_Student(prn, fname, mname, lname, clas, dept, batch, scontact, pcontact, email, password, dob);
                        Toast.makeText(ct, "JSON Object:" + st.fname, Toast.LENGTH_SHORT).show();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(ct, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(ct, error.getMessage(), Toast.LENGTH_SHORT).show();  // hide the progress dialog

            }
        });

        req.add(jsonObjReq);

        //Toast.makeText(ct,"DO in back End"+st.fname,Toast.LENGTH_SHORT).show();
        return st;
    }

}
kopikaokao
  • 500
  • 7
  • 13
  • This might help http://stackoverflow.com/questions/28120029/how-can-i-return-value-from-function-onresponse-of-volley – Jiyeh Jan 22 '16 at 04:26

2 Answers2

0

You can't return from anonymous inner classes, but you could create a method inside ServerRequests that takes a user_Student as a parameter and call that method from within onResponse. This method could then do whatever you need.

TheoKanning
  • 1,631
  • 11
  • 20
0

You must use AsyncTask to do funtion doInBackground(user_Student user)

You can view this post to understand AsyncTask: How to use AsyncTask correctly in Android

Community
  • 1
  • 1