使用Volley库使用GET方法传递参数 [英] Pass params Using GET method using volley library

查看:144
本文介绍了使用Volley库使用GET方法传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 request.GET 方法将参数从editText传递到 url .实际上,我正在尝试将电子邮件地址作为参数传递给 api ,该参数应b附加到 api-url .

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 .

我从此处得知在GET方法上没有调用getParams(),因此似乎您必须在发送请求之前将其添加到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 ..

当我通过REG_URL ="http://ec2-54-147-238-136.compute-1.amazonaws.com/hmc/api/registeruser?email=ameer@novatoresols.com"时;由于已注册用户,它返回成功= true预期的响应但是如果我设置REG_URL ="http://ec2-54-147-238-136.compute-1.amazonaws.com/hmc/api/registeruser并传递参数(从edittext获取值并在getparams中使用params.put()方法).response始终为success = false,即参数未附加到url
这是我的代码.

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


    }
}

推荐答案

@Puneet的建议对我有用,即:

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

getParams . GET 请求没有正文,因此,永远不会调用 getParams .对于像您这样的简单请求,只需将参数添加到您的 URL 中,然后使用构造好的 URL 将请求发送到服务器(REG_URL +?email ="+电子邮件).

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

这篇关于使用Volley库使用GET方法传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆