你好我想字符串值数组在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变量可以) –