如何在Volley中发送带有数据的邮件请求?[英] how to send post request with data in Volley?

本文是小编为大家收集整理的关于如何在Volley中发送带有数据的邮件请求?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

这是我的android代码

           JSONObject params = new JSONObject();
           params.put("username","rajesh@gmail.com");
           params.put("password","hellothere");

           JsonObjectRequest loginRequest = new JsonObjectRequest(
                    Request.Method.POST,
                    "http://192.168.2.67/tmp/test.php",
                    params,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject jsonObject) {
                        Log.d("","");

                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {
                        Log.d("","");
                        }
                    });


            requestQueue.add(loginRequest);

PHP中的服务器代码

            <?php
               $usr = $_REQUEST['username'];
                $pwd = $_REQUEST['password'];
                $resp[$usr]=$pwd;
                echo json_encode($resp);
               ?>

我得到的回应是 {"":null}

我尝试了apache http letint,它普拉的工作 有没有办法,我可以用凌空做这个吗?

推荐答案

在请求身体中发送参数,您可以覆盖 getparams()或 getbody()方法的请求类

源:异步HTTP请求在Android中使用凌射

其他推荐答案

@Override
protected Map<String, String> getParams() 
{  
Map<String, String>  params = new HashMap<String, String>();  
    params.put("username", "SomeUser");  
    params.put("password", "blabla");



 return params;  
}

这是您应该覆盖getparams()方法的方式.

本文地址:https://www.itbaoku.cn/post/102553.html

问题描述

This is my android code

           JSONObject params = new JSONObject();
           params.put("username","rajesh@gmail.com");
           params.put("password","hellothere");

           JsonObjectRequest loginRequest = new JsonObjectRequest(
                    Request.Method.POST,
                    "http://192.168.2.67/tmp/test.php",
                    params,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject jsonObject) {
                        Log.d("","");

                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {
                        Log.d("","");
                        }
                    });


            requestQueue.add(loginRequest);

The server code in php

            <?php
               $usr = $_REQUEST['username'];
                $pwd = $_REQUEST['password'];
                $resp[$usr]=$pwd;
                echo json_encode($resp);
               ?>

the response i'm getting is {"":null}

I tried with apache http cleint and it worked prferectly is there any way i can do this with volley?

推荐答案

To send parameters in request body you need to override either getParams() or getBody() method of the request classes

Source: Asynchronous HTTP Requests in Android Using Volley

其他推荐答案

@Override
protected Map<String, String> getParams() 
{  
Map<String, String>  params = new HashMap<String, String>();  
    params.put("username", "SomeUser");  
    params.put("password", "blabla");



 return params;  
}

This is the way you should override getParams() method.