0

How can we pass params from editText to url using request.GET method. Actually I am trying to pass an email address as parameter to a api which should b attached to api-url .

I came to know from here that getParams() is not called on the GET method, so it seems you'll have to add it to the URL before you send the request. suggest me any solution to achieve the task ..

when i pass REG_URL="http://ec2-54-147-238-136.compute-1.amazonaws.com/hmc/api/registeruser?email=ameer@novatoresols.com"; it return success=true response as expected because is registered user but if i set REG_URL="http://ec2-54-147-238-136.compute-1.amazonaws.com/hmc/api/registeruser and pass the params (get value from edittext and use params.put in getparams() method ).response is always success=false i.e params is not attached to url
here is my code.

    package com.example.mts3.hammadnewsapp;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.provider.SyncStateContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class RegisterActivity extends AppCompatActivity {

    Button btn_verf;
    EditText et_Email;
    String u_emails,stat;
    AlertDialog.Builder alertDialog;
    private static final String TAG = "LoginActivity";
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;

    Context context;
//    public static String firstname, lastname, useremail, userphone, userpass;


//    String REG_URL="http://ec2-54-147-238-136.compute-1.amazonaws.com/hmc/api/registeruser?email=ameer@novatoresols.com";
    String REG_URL="http://ec2-54-147-238-136.compute-1.amazonaws.com/hmc/api/registeruser";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        btn_verf=findViewById(R.id.btn_reg_send_vf_code);
        et_Email=findViewById(R.id.et_reg_email);
        alertDialog =new AlertDialog.Builder(RegisterActivity.this);
//        u_emails=et_Email.getText().toString().trim();


        btn_verf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callApi();
            }
        });
    }

    private void callApi() {
//        Log.e(TAG, "onClick: ");
        /*if (!utility.isInternetConnected()) {
            Toast.makeText(LoginActivity.this, "Please check your internet connection.", Toast.LENGTH_SHORT).show();
            return;
        }*/
//        dialog = utility.showProgressDialog(LoginActivity.this, "Please wait");
        final String email = et_Email.getText().toString().trim();

//        Log.e(TAG, "onClick: email = " + email );

//        JSONObject params = new JSONObject();
/*
        HashMap<String,String> params=new HashMap<>();
        params.put("email",email);*/
        /*try {
//            params.getString("email");
            params.put("email",email);
            Log.e(TAG, "getParams: param = " + "try of put prams");
        } catch (JSONException e){
            Log.e(TAG, "getParams: param = " + "catch of put prams");
            e.printStackTrace();
        }*/
        RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);



        StringRequest stringRequest = new StringRequest(Request.Method.GET, REG_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Toast.makeText(RegisterActivity.this, "REsponse: " + response, Toast.LENGTH_SHORT).show();
            }


        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String,String> params=new HashMap<>();
//                params.put("email",email);
                params.put("email",email);
                return params;

            }
        };        queue.add(stringRequest);


    }
}
Muahmmad Tayyib
  • 689
  • 11
  • 28

2 Answers2

4

As suggested by @Puneet worked for me which is as :

getParams is only called for POST requests. GET requests don't have a body and hence, getParams is never called. For a simple request like yours just add the parameters to your URL and use that constructed URL to make that request to your server (REG_URL + "?email=" + email).

Xenolion
  • 12,035
  • 7
  • 33
  • 48
Muahmmad Tayyib
  • 689
  • 11
  • 28
0

To pass the parameters, you need to create a class for the key-value pairs.

1) Create a class KeyValuePair with two fields key and value with appropriate constructor and getter-setter methods.

2) Now, for each parameter, you need to create an object of this class, i.e., for a key username with value user@gmail.com, the object would be new KeyValuePair("username", "user@gmail.com").

3) Now, you need to create a List to store these parameters and pass this list to the below method with your base url,

 public static String generateUrl(String baseUrl, List<KeyValuePair> params) {
    if (params.size() > 0) {
        for (KeyValuePair parameter: params) {
            if (parameter.getKey().trim().length() > 0)
                baseUrl += "&" + parameter.getKey() + "=" + parameter.getValue();
        }
    }
    return baseUrl;
}

4) Pass this baseUrl to your Request.

Jay
  • 2,852
  • 1
  • 15
  • 28