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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)