douwu0335 2016-06-13 07:50
浏览 58
已采纳

如何在PHP中通过Volley发送PHP中的HashMap?

I am working on an android application and am trying to post 'username' and 'password' parameters to a server to be processed.

I have done this in Java using volley to send a hashMap to the server as I found out how to do via external research.

final HashMap<String, String> dataToSend = new HashMap<>(); //Contains key/value pairs of data we will send to the server
                                    //Add the data to the data package
                                    dataToSend.put("username", username); //Add username to the package
                                    dataToSend.put("password", password); //Add password to the package
                                    final String url = "url here"; //The url we will make the delivery to.

                                    //Making the object request, parsing the data to send to the server
                                    JsonObjectRequest loginRequest = new JsonObjectRequest(Request.Method.POST, url, new JSONObject(dataToSend), new Response.Listener<JSONObject>() {
                                        @Override
                                        public void onResponse(JSONObject response) {
                                            login_progress_dialog.dismiss();
                                            try {
                                                Toast.makeText(getContext(), "You said: " + response.getString("username"), Toast.LENGTH_SHORT).show();
                                            }
                                            catch (Exception e){
                                                Toast.makeText(getContext(), e.toString(), Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                    }, new Response.ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError error) {
                                            //An error occurred during transmission, log to console and notify user.
                                            login_progress_dialog.dismiss();
                                            Toast.makeText(getContext(), error.toString(), Toast.LENGTH_SHORT).show();
                                        }
                                    });

                                    RequestQueue loginRequestQueue = Volley.newRequestQueue(getContext());
                                    loginRequestQueue.add(loginRequest); //Add the above request to the queue for execution.

I then have my PHP script on the server, called from volley via https in url. At the moment all I need to do is get the username and password out of the hashMap without using GET for security reasons.

I have no idea how to do that and am unable to find any help online. Here is PHP as it stands at the moment:

<?php
    $username = $_POST["username"];
    $response = json_encode(array('username' => $username)); 
    echo $response; 
?>

The above always returns null? How would I get the data into the PHP script from the HashMap? I think the issue is with my PHP but I am unsure.

  • 写回答

1条回答 默认 最新

  • dtyw10299 2016-06-13 08:04
    关注

    I think you should overide the below method in request like below

     @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<String, String>();
                params.put(KEY_USERNAME,username);
                params.put(KEY_PASSWORD,password);
                params.put(KEY_EMAIL, email);
                return params;
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法