0

I am creating an android app with the Google Volley Api that handles the connection between my app and my php script. For some reason, the app does not send any data to my server, maybe some one here can figure it out?

My getParams() method is called correctly, but when I try to print it, it returns null! Everything is actually working, except for that data is not passed correctly I believe, since my php script complains about variables that are null

public class Server_interaction
{
    String server_url = "http://xxxxxx/update_location.php"; //hidden from you ;)
String response_string;
RequestQueue queue;
Context context;

public Server_interaction(Context context)
{
     this.context = context;
     queue = Server_singleton.getInstance(context).getRequestQueue();
}


public static final String TAG = Server_interaction.class.getSimpleName();

public void post_request(final VolleyCallback callback)
{
    StringRequest stringRequest = new StringRequest(Request.Method.POST, server_url,
            new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response)
                {
                    response_string = response;
                    callback.onSuccess(response_string);
                    //requestQueue.stop();
                    Log.i(TAG, "the response is: "+ response_string);

                }
            }
            , new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error){
                    response_string = "Something went wrong";
                    //error.printstacktrace()
                    //requestQueue.stop();
                    Log.i(TAG, "something went wrong. Is the server up and running?");
                }

            })
    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {

            String the_name = "olaf";
            String the_mail = "lalalal";
            String the_country = "Norway";
            String the_latitude = "33";
            String the_longitude = "99";


            Map<String, String> params = new HashMap<String, String>();
            params.put("name", the_name);
            params.put("email", the_mail);
            params.put("country", the_country);
            params.put("latitude", String.valueOf(the_latitude));
            params.put("longitude", String.valueOf(the_longitude));

            Log.i(TAG, "inside getparams : "+ super.getParams());
            return super.getParams();
        }
    };//stringrequest parameter end

    //add request to requestqueue
    Server_singleton.getInstance(context).addToRequestQueue(stringRequest);
    Log.i(TAG, "the response again:: "+ response_string);


}

}
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
p3ob2lem
  • 129
  • 2
  • 11
  • Put important pieces of information like the getParams in the original question. Without that it sounds like your request queue isn't running, which means you failed to call start(). – Gabe Sechan Feb 01 '17 at 21:54
  • I call a singleton class, I believe its all correcty done. However, my php server side script gets empty(null) values, and that should the getparams.put methods prevent? Should they not? – p3ob2lem Feb 01 '17 at 21:56
  • Any idea Gabe?? – p3ob2lem Feb 02 '17 at 15:00
  • This problem is solved in [volley post issues](https://stackoverflow.com/a/52981133/7547389) – Ryan Naccarato Oct 25 '18 at 04:03

2 Answers2

1
   Map<String, String> params = new HashMap<String, String>();
    params.put("name", the_name);
    params.put("email", the_mail);
    params.put("country", the_country);
    params.put("latitude", String.valueOf(the_latitude));
    params.put("longitude", String.valueOf(the_longitude));

    Log.i(TAG, "inside getparams : "+ super.getParams());
    return super.getParams();

YOu're adding all your params, then you're ignoring them and calling super.getParams. You either want to return params instead of super.getParams(), or merge the two results. Since your parent is just StringRequest, you can just return params here (StringRequest won't add any on its own).

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Thanks, but now I get a huge error and my app crashes! Seems like my params method is called thousands of times, before it crashes. Produces a huge stacktrace, this is the first bit: FATAL EXCEPTION: Thread-166 Process: com.example.koenraad.Exigentia, PID: 7288 java.lang.StackOverflowError: – p3ob2lem Feb 01 '17 at 22:02
  • Sounds like you miscoded it and went into infinite recursive loop. But since you didn't post new code or the full error, I can't tell you how to fix it. But my guess is you wrote return getParams() instead of return params; – Gabe Sechan Feb 01 '17 at 22:05
  • I am such an idiot. Haha correct. Strangely though, my php server side still complains about not receiving the data. The data is just null/false. I find it very odd, since the strings are so hardcoded right before I use them in the .put method. Now params contains the right values, and still they are not posted correctly? – p3ob2lem Feb 01 '17 at 22:11
1

Try to modify your code to this:

  StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Toast.makeText(ReserveProperty.this, s, Toast.LENGTH_SHORT);



            }
        }, new Response.ErrorListener(){

            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(ReserveProperty.this, volleyError.toString(),Toast.LENGTH_LONG).show();

            }
        }){
            protected Map<String,String> getParams()
            {
                  String the_name = "olaf";
        String the_mail = "lalalal";
        String the_country = "Norway";
        String the_latitude = "33";
        String the_longitude = "99";


        Map<String, String> params = new HashMap<String, String>();
        params.put("name", the_name);
        params.put("email", the_mail);
        params.put("country", the_country);
        params.put("latitude", String.valueOf(the_latitude));
        params.put("longitude", String.valueOf(the_longitude));

                return params;


            }
        };


        Volley.newRequestQueue(this).add(stringRequest);



    }
Lutaaya Huzaifah Idris
  • 3,596
  • 8
  • 38
  • 77