Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How do i pass 2 or more variables in a URL

    • 0
    • 0
    • 0
    • 4
    • 0
    • 0
    • 0
    • 413
    Answer it

    private void doAssign() { String nameStr = fname.getText().toString(); String phoneStr = phone.getText().toString(); String bikeStr = bike.getText().toString();

        String details = nameStr + "," + phoneStr + "," + bikeStr;
        RequestQueue queue = VolleySingleton.getsInstance().getRequestQueue();
    
    
    
    //     String url = "http://vvvv.com/vvv/campus/campus_assign.php?id=' " + id + " '& details=' " + details + "'";
    

    // i want to pass two variables in the URL like this but it is not working how do i do that?

        CustomRequest jsonObjectRequest = new CustomRequest(Request.Method.POST, url, params, new Response.Listener<JSONObject>() {
    
    
            @Override
            public void onResponse(JSONObject response) {
    
                Log.d("Response: ", response.toString());
    
    
            }
        }, new Response.ErrorListener() {
    
            @Override
            public void onErrorResponse(VolleyError response) {
                Log.d("Response: ", response.toString());
            }
        }
        );
        queue.add(jsonObjectRequest);
    }
    

 4 Answer(s)

  • Uri.Builder builder = new Uri.Builder();
    builder.scheme("https")
            .authority("abc.com")
            .appendPath("xyz")
            .appendQueryParameter("Variable1", var1)
            .appendQueryParameter("Variable2", var2);
    
    String url = builder.build().toString();
  • Hey ,

    If you are making get request you can just append to the url itself with & for example

    1. MYURL.apiUrl+"api/v1/resolvedcommand?q="+ Uri.encode(command)+"&apiKey="+ MYURLBUILDER.apiKey;

    in above example command is the value which is being passed as a parameter.

  • Yes you can post variables in volley api like this.

    you have to create HashMap that will contain key value pair for each variable

     HashMap<String, String> params = new HashMap<String, String>();
    
     params.put("key3", "value");   
     params.put("key4", "value");
    
        JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params),
    
           new Response.Listener<JSONObject>() {
               @Override
               public void onResponse(JSONObject response) {
    
                   Log.e("Response:%n %s", response.toString());
    
               }
           }, new Response.ErrorListener() {
               @Override
               public void onErrorResponse(VolleyError error) {
    
                  Log.e("Error: ", error.getMessage());
    
               }
           });
    

    Hope this will help you.

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: