简体   繁体   English

使用Android Volley将各种数据类型从android发送到php

[英]Send various data types from android to php using android volley

I have an android app. 我有一个Android应用程序。 I want to send POST request to a php file. 我想将POST请求发送到一个php文件。 The parameters consist of arrayList and Strings. 参数由arrayList和Strings组成。 I want to do something like this: 我想做这样的事情:

private Map<String, String> params;

    public RequestPostBook(long id, ArrayList<String> bookNames, ArrayList<String> bookAuthors, ArrayList<String> subjects,
                           String date,Response.Listener<String> listener)
    {
        super(Method.POST, REGISTER_REQUEST_URL, listener, null);


        params = new HashMap<>();
        params.put("candidateId", id + "");
        params.put("bookName",bookNames);
        params.put("authorName",bookAuthors);
        params.put("subjectName",subjects);
        params.put("reqDate",date);
    }

    @Override
    public Map<String, String> getParams() {
        return params;
    }

This is giving errors as the Map is (String, String).. Please help me about how to get it done. 映射为(String,String),这会产生错误。.请帮助我了解如何完成它。 Please rectify this code. 请更正此代码。 Thanks in advance. 提前致谢。

The problem is that your Map is composed of <String,String> and you are passing an ArrayList<String> . 问题是您的Map<String,String>组成,并且您正在传递ArrayList<String> You should do something like bookNames.toString() then you'll send a String 您应该执行类似bookNames.toString()然后发送一个String

Or also you can create a JSONArray as follows : 或者您也可以创建一个JSONArray ,如下所示:

JSONArray types=new JSONArray(Arrays.asList.(bookNames));

And then do the types.toString() and should work. 然后执行types.toString()并且应该可以工作。

Then to decode it with PHP you should use json_decode function 然后使用PHP对其进行解码,您应该使用json_decode函数

$array = json_decode($json);

params is a Map<String, String> , so it can only take String as values. params是Map<String, String> ,因此它只能将String作为值。 You are trying to put ArrayList s into it. 您正在尝试将ArrayList放入其中。 What you might want to try is 您可能想尝试的是

JSONArray bookNamesJSON = new JSONArray(Arrays.asList(bookNames));
JSONArray bookAuthorsJSON = new JSONArray(Arrays.asList(bookAuthors));
JSONArray subjectsJSON = new JSONArray(Arrays.asList(subjects));

and then pass these objects to params 然后将这些对象传递给params

params.put("bookName",bookNamesJSON.toString());
params.put("authorName",bookAuthorsJSON.toString());
params.put("subjectName",subjectsJSON.toString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM