简体   繁体   中英

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.

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);

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM