0

I Wrote A program that connect to server and login with my username and password .it must return first name and last name in table to app too. i wrote these codes in volley it work when i see break point . but it has delay in on response method .i tested interface too but it does not work. when i run it show me null but i see correct answer in log . how can i do this correctly ? help me please...

 public void logInAdminToServer(JSONObject jsonObject, final VolleyCallback callback) {

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,

            "http://192.168.43.206/project-server/Admin.php?action=LogInAdmin", jsonObject,

            new Response.Listener<JSONObject>() {

                @Override

                public void onResponse(JSONObject response) {

                    if (response != null) {

                        try {

                            Log.i("Error", response.getString("AdminFirstName") + response.getString("AdminLastName"));

                            callback.OnSuccess(response.getString("AdminFirstN  ame") + "," + response.getString("AdminLastName"));

                        } catch (JSONException e) {

                            e.printStackTrace();

                        }

                    }

                }

            },

            new Response.ErrorListener() {

                @Override

                public void onErrorResponse(VolleyError error) {

                    Log.i("Error", error.toString());

                }

            });

    request.setRetryPolicy(new DefaultRetryPolicy(5000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

    Volley.newRequestQueue(G.context).add(request);

}



public interface VolleyCallback {

    void OnSuccess(String result);

}

another block of code to get data in a class

public void logInAdmin() {

    try {

        object.put("AdminUserName", adminUserName);

        object.put("AdminPassword", adminPassword);

    } catch (JSONException ex) {

        ex.printStackTrace();

    }

    //

    //basic login operation

    //

    thread = new Thread(new Runnable() {

        @Override

        public void run() {

            apiService.logInAdminToServer(object, new ApiService.VolleyCallback() {

                @Override

                public void OnSuccess(String result) {

                    if (result != null) {

                        String str[] = result.split(",");

                        adminFirstName = str[0];

                        adminLastName = str[1];

                    }

                }

            });

        }

    });

    thread.start();

}

why i get null answer but show correct answer in log .please help me . it makes me crazy .

0 Answers0