Simon Wanjau.
Simon Wanjau.

Reputation: 173

How to send strings using volley library?

Android programming: SENDING DATA TO SERVER USING VOLLEY

Am new to the volley library.

I have the following EditText where user enters data and presses the register button. I want to send the data to a remote server hosted at hostinger.

EditText firstname  =  (EditText)findViewById(R.id.firstname);
EditText lastname   =  (EditText)findViewById(R.id.lastname);
EditText email      =  (EditText)findViewById(R.id.email);
EditText phone      =  (EditText)findViewById(R.id.phone);
EditText password   =  (EditText)findViewById(R.id.password);
EditText repeatpass =  (EditText)findViewById(R.id.rptpassword);

Button regbtn    =  (Button)findViewById(R.id.regbtn);
Button tologin   =  (Button)findViewById(R.id.loginbtn);

How do i post the data to server?

Kindly include the code for the server side.

Upvotes: 0

Views: 474

Answers (1)

DevOps85
DevOps85

Reputation: 6523

A simple String request:

RequestQueue queue = MyVolley.getRequestQueue();
                StringRequest myReq = new StringRequest(Method.GET, 
                                                        "http://www.google.com/",
                                                        createMyReqSuccessListener(),
                                                        createMyReqErrorListener());

queue.add(myReq);

}

private Response.Listener<String> createMyReqSuccessListener() {
        return new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {


               //TODO

            }
        };
    }


    private Response.ErrorListener createMyReqErrorListener() {
        return new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                  //TODO
            }
        };
    }

And in the MyVolley class:

public static RequestQueue getRequestQueue() {
        if (mRequestQueue != null) {
            return mRequestQueue;
        } else {
            throw new IllegalStateException("RequestQueue not initialized");
        }
    }

If you want to add String params is simple:

String uri = "www.google.com" + params;

RequestQueue queue = MyVolley.getRequestQueue();
                    StringRequest myReq = new StringRequest(Method.GET, 
                                                            uri,
                                                            createMyReqSuccessListener(),
                                                            createMyReqErrorListener());

    queue.add(myReq);

    }

Upvotes: 1

Related Questions