代码之家  ›  专栏  ›  技术社区  ›  RFA

ANDROID-通过截击向PHP发送列表

  •  1
  • RFA  · 技术社区  · 8 年前

    您好,我有一个名为StudentBATCH\u List的列表,我使用csv中的内容填充该列表:

        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);
        }
    

    我想知道如何通过截击将此列表传递给PHP。我该怎么做?还有,我如何在PHP中对其进行解码。

    无论如何,列表中值的标记为 USER_ID , STUD_FIRSTNAME , STUD_LASTNAME , STUD_MIDDLENAME , STUD_EMAIL

    1 回复  |  直到 8 年前
        1
  •  1
  •   Natig Babayev Braj    8 年前

    为了应用程序的目的,我将StudentBATCH\u列表转换为JSON数组,然后在web api上反序列化该数组。

    以下是简要步骤:

    第1步。将Volley和GSON依赖项添加到应用程序级构建中。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步。将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.
    }