2016-06-10 86 views
1

我想從一個volley requset發送一個ID和數組列表的字符串數據給php。但我不知道如何可以正確發送到服務器,以及如何可以在PHP中。 下面是Android的一面請求發送到服務器:如何從凌亂請求發送arraylist數據並獲取php

private void sendMessage() { 

StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.NOTIF_URL, 
    new Response.Listener <String>() { 
    @Override 
    public void onResponse(String response) { 

    Log.d("Response --->", response); 
    jsonNotif = new ParseJSON(response); 
    jsonNotif.parseJSON(); 

    } 
    }, 
    new Response.ErrorListener() { 
    @Override 
    public void onErrorResponse(VolleyError error) { 
    Toast.makeText(context, error.getMessage(), Toast.LENGTH_LONG).show(); 
    } 
    }) { 
    @Override 
    protected Map < String, String > getParams() throws AuthFailureError { 
    Map < String, String > params = new HashMap < >(); 
    //Adding parameters to request 

    ArrayList <String> courseList = new ArrayList <String> (checkedSet); 

    String ID = prefProfID.getString(Config.PROFID_SHARED_PREF, "0"); 
    Log.d("ID prof list >>", ID); 
    params.put(Config.PROFID_SHARED_PREF, ID); 


    for (int i = 0; i < courseList.size(); i++) { 
    params.put("courselist", courseList.get(i)); 
    } 
    //returning parameter 
    return params; 
    } 
}; 
RequestQueue requestQueue = Volley.newRequestQueue(context); 
requestQueue.add(stringRequest); 

} 

這裏是我的PHP代碼:

<?php 
if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    //Getting values 
    $courseList = $_POST['courseList']; 
    $professor_ID = $_POST['Prof_ID']; 
    require_once('dbConnect.php'); 
    $newcourseList = implode(", ", $courseList); 
    $sql   = "select Stud_ID,student.f_Name,student.l_Name from student,course  where course.S_ID = student.Stud_ID and course.P_ID in ($newcourseList)"; 
    $res = mysqli_query($con, $sql); 
    $result = array(); 
    while ($row = mysqli_fetch_array($res)) { 
     array_push($result, array(
      'id' => $row[0], 
      'fname' => $row[1], 
      'lname' => $row[2], 
      'tag' => 'studlist' 
     )); 
    } 
    echo json_encode(array(
     "result" => $result 
    )); 
    mysqli_close($con); 
} 
?> 
+0

將其轉換爲JSON格式和發送的字符串 – Sush

回答

1

如果你想發送的ArrayList的數據,我認爲其更好地將其轉換爲發送成JSONArray

1

首先在你的對象的ArrayList中:創建JSONObjectmethod名getJSONObject,這樣

public class EstimateObject { 
String id, name, qty, price, total; 
public EstimateObject(String id, String name, String qty, String price, String total, int position) 
{ 
    this.id = id; 
    this.name = name; 
    this.qty = qty; 
    this.price = price; 
    this.total =total; 
    this.position = position; 
} 
public JSONObject getJSONObject() { 
    JSONObject obj = new JSONObject(); 
    try { 
     obj.put("Id", id); 
     obj.put("Name", name); 
     obj.put("Qty",qty); 
     obj.put("Price", price); 
     obj.put("Total", total); 
    } 
    catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    return obj; 
} 

Aftrer這是我如何轉換它,在我的活動

JSONObject JSONestimate = new JSONObject(); 
    JSONArray myarray = new JSONArray(); 

    for (int i = 0; i < items.size(); i++) { 

     try { 
      JSONestimate.put("data:" + String.valueOf(i + 1), items.get(i).getJSONObject()); 
      myarray.put(items.get(i).getJSONObject()); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
    Log.d("JSONobject: ", JSONestimate.toString()); 
    Log.d("JSONArray : ", myarray.toString()); 

在這裏,我既轉換型的JSONObject和JSONArray。

經過

map.put("jsonarray",myarray.toString()); 

而在PHP端:

$json = $_POST['jsonarray']; 
$jsonarray = json_decode($json,true);