Search code examples
androidjsongrailsandroid-intenttoast

Android does not Toast after successful POST


I am sending data from an android client to a grails backend. I want two things to happen when data is successfully posted:

  1. A toast with some of the params I have posted(In this case I want only name)
  2. An intent to open another activity.

However with my code, the parameters are posted successfully to the db but the toast and the intent do not happen.

Here is my code:

 */
    private void registerUser(final String chname, final String chdesc, final String patientID) {
        // Tag used to cancel the request
        String tag_string_req = "req_register";

        pDialog.setMessage("Registering ...");
        showDialog();

        StringRequest strReq = new StringRequest(Method.POST,
                Configs.URL_LOGIN, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Register Response: " + response.toString());
                hideDialog();

                try {
                    JSONObject jObj = new JSONObject(response);
                    boolean error = jObj.getBoolean("error");
                    if (!error) {
                        // User successfully stored in MySQL
                        // Now store the user in sqlite
                        String uid = jObj.getString("uid");

                        JSONObject user = jObj.getJSONObject("user");
                        String chname = user.getString("name");
                        String chdesc = user.getString("description");
                        String patientID = user.getString("patient");



                        Toast.makeText(getApplicationContext(), "User successfully registered.", Toast.LENGTH_LONG).show();

                        // Launch login activity
                        Intent intent = new Intent(AddChronics.this,
                                ChronicsFragment.class);
                        startActivity(intent);
                        finish();
                    } else {

                        // Error occurred in registration. Get the error
                        // message
                        String errorMsg = jObj.getString("error_msg");
                        Toast.makeText(getApplicationContext(),
                                errorMsg, Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Registration Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_LONG).show();
                hideDialog();
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                // Posting params to activity_register url
                Map<String, String> params = new HashMap<String, String>();
                params.put("name", chname);
                params.put("description", chdesc);
                params.put("patient", patientID);

                params.put("action", "chronicAdd");

                return params;


            }


        };



        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }

Note: The API works just fine. I have checked and the params are successfully saved to the db.


Solution

  • Your code looks fine, the only thing that might prevent the intent and toast from happening is if your code gets cought by an excepetion here:

        catch (JSONException e) {
          e.printStackTrace();
     }
    

    Caused by one of these guys:

         String uid = jObj.getString("uid");
         JSONObject user = jObj.getJSONObject("user");
    

    Did you check that?