你好我想字符串值數組在PHP發送到PHP服務器和PHP解碼,並將其儲存在Android發送陣列和接收在PHP服務器變量使用凌空
這是我在機器人工作室
代碼private void getEventDetailRespond(RequestQueue requestQueue) {
JSONObject params = new JSONObject();
try {
for (int i=0; i <eventIDBeacon.size();i++){
params.put(Config.EVENT_ID, eventIDBeacon.get(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
//Creating a JSONObject request
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,Config.DATA_URL,params.toString(),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject respond) {
try {
Toast.makeText(Beacon_MainActivity.this,"eventDetail respond "+respond.toString(),Toast.LENGTH_LONG).show();
eventArray = new JSONArray();
eventDetail = new ArrayList<>();
eventArray = respond.getJSONArray("result");
eventDetail = getEventDetail(eventArray);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Beacon_MainActivity.this, "Unable to fetch data event Detail: " +error.getMessage(),Toast.LENGTH_LONG).show();
}
}
);
//Adding request to the queue
requestQueue.add(jsonObjectRequest);
}
private ArrayList getEventDetail(JSONArray j) {
ArrayList event = new ArrayList();
//Traversing through all the items in the json array
for (int i = 0; i < j.length(); i++) {
try {
//Getting json object
JSONObject json = j.getJSONObject(i);
//Adding the name of the event to array list
event.add(json.getString(Config.EVENT_TITLE));
} catch (JSONException e) {
e.printStackTrace();
}
}
if (event.isEmpty()) eventView.setVisibility(View.INVISIBLE);
else {
if (beacons.size()!=0) {
checkIn.setVisibility(View.VISIBLE);
eventView.setVisibility(View.VISIBLE);
spinner.setAdapter(new ArrayAdapter<String>(Beacon_MainActivity.this, android.R.layout.simple_spinner_dropdown_item, event));
}else {
checkIn.setVisibility(View.INVISIBLE);
eventView.setVisibility(View.INVISIBLE);
}
}
return event;
}
而且從PHP大小收到,這是我的代碼
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
// decoding the json array
$post = json_decode(file_get_contents("php://input"), true);
$eventID = $post['EventID'];
require_once('dbconnect.php');
$sql = "SELECT EventID, EventTitle, EventDesc, EventTime FROM Event WHERE EVENTID = '$eventID'";
$res = mysqli_query($con,$sql);
$result = array();
while ($row = mysqli_fetch_array($res)){
array_push($result,array(
'EventID'=>$row['EventID'],
'EventTitle'=>$row['EventTitle'],
'EventDesc'=>$row['EventDesc'],
'EventTime'=>$row['EventTime']
));
}
header('Content-Type: application/json');
echo json_encode(array('result'=>$result), 256);
mysqli_close($con);
}
它似乎沒有工作,因爲我無法發送和數組到PHP服務器,它PHP服務器上進行解碼。任何幫助非常感謝。
感謝了很多,但如何
後解碼PHP服務器上並存儲該值到PHP數組? –
是否要將「$ post」變量存儲到PhP數組中? – tsiro
是的我的目標是發送ArrayList(eventBeacon)到PhP服務器並存儲到本地PhP變量中。我仍然不知道如何發送和接收和數組(1變量可以) –