ANDROID - 通过 VOLLEY 将列表发送到 PHP [英] ANDROID - send List to PHP via VOLLEY

查看:36
本文介绍了ANDROID - 通过 VOLLEY 将列表发送到 PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个名为 StudentBATCH_LIST 的列表,我使用 csv 中的内容填充了该列表:

Hello I have a List named StudentBATCH_LIST that I populated with the contents from a csv using:

    File csvFILE = new File(getFILE_PATH());
    try {
        CSVReader csvREAD = new CSVReader(new 
        FileReader(csvFILE.getAbsolutePath()));
        String[] csvLINE;
        int skip = 0;
        while((csvLINE = csvREAD.readNext())!=null)
        {
            if(skip > 0)//becasue first line is column headers
            {
                String PARAM_USER_ID = csvLINE[0];
                String PARAM_STUD_FIRSTNAME = csvLINE[1];
                String PARAM_STUD_LASTNAME = csvLINE[2];
                String PARAM_STUD_MIDDLENAME = csvLINE[3];
                String PARAM_STUD_EMAIL = PARAM_USER_ID+EMAIL_S;

                AdminAddStudentBATCH_CONFIG STUD_OBJECT = new AdminAddStudentBATCH_CONFIG(PARAM_USER_ID,
                        PARAM_STUD_FIRSTNAME,
                        PARAM_STUD_LASTNAME,
                        PARAM_STUD_MIDDLENAME,
                        PARAM_STUD_EMAIL);
                StudentBATCH_LIST.add(STUD_OBJECT);
            }
            else
            {
                skip ++;
            }
        }

    } catch (FileNotFoundException e) {
        volleyErrorClass.catchInvalidResponse(e.toString(),AdminAddStudentBATCH.this);
    } catch (IOException e) {
        volleyErrorClass.catchInvalidResponse(e.toString(),AdminAddStudentBATCH.this);
    } catch (JSONException e) {
        volleyErrorClass.catchInvalidResponse(e.toString(),AdminAddStudentBATCH.this);
    } catch (IndexOutOfBoundsException e) {
        volleyErrorClass.catchInvalidResponse(e.toString(),AdminAddStudentBATCH.this);
    }

我想知道如何通过 volley 将此列表传递给 PHP.我该怎么做?还有我如何在 PHP 中解码它.

I want to know how do I pass this List to PHP with volley. How do I do it? Also how do I decode this in PHP.

反正List中值的标签是USER_ID,STUD_FIRSTNAMESTUD_LASTNAMESTUD_MIDDLENAME,STUD_EMAIL

Anyway the tags of the values in the List are USER_ID, STUD_FIRSTNAME, STUD_LASTNAME, STUD_MIDDLENAME, STUD_EMAIL

推荐答案

为了您的应用程序的目的,我会将 StudentBATCH_LIST 转换为 JSON 数组,然后在您的 web api 上反序列化该数组.

For the purpose of your application, I'd convert the StudentBATCH_LIST to JSON array, and then deserialize that array on your web api.

以下是简要步骤:

步骤 1. 将 Volley 和 GSON 依赖项添加到应用级 build.gradle 文件:

dependencies {
    ...
    compile 'com.android.volley:volley:1.1.0'
    compile 'com.google.code.gson:gson:2.8.2'
}

步骤 2. 使用 GSON 将列表转换为 JSON 数组:

String studentBatchListString = new Gson().toJson(students);

步骤 3. POST studentBatchListString 到您的 web api(发送到 PHP")

String url = "http://yourdomain.com/post.php";
StringRequest postRequest = new StringRequest(Request.Method.POST, url, 
    new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            // response
            Log.d("Response", response);
        }
    }, 
    new Response.ErrorListener() {
         @Override
         public void onErrorResponse(VolleyError error) {
             // error
             Log.d("Error.Response", response);
       }
    }
) {     
    @Override
    protected Map<String, String> getParams() {  
            Map<String, String>  params = new HashMap<String, String>();  
            params.put("students_batch_list", studentBatchListString);  
            return params;  
    }
};
queue.add(postRequest);

第 4 步:在 PHP 端反序列化:

$content = $_POST['students_batch_list'];
$json = json_decode($content, true);
foreach ($json as $key => $value) {
    $firstname = $value["firstname"];
    $lastname = $value["lastname"];
    // perform other actions.
}

这篇关于ANDROID - 通过 VOLLEY 将列表发送到 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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