目的:Flex与Android共享Amfphp SERVICES 下自己的代码
由于amfphp(https://www.silexlabs.org/amfphp/),暂时不能提供Amfphp和Android的接口,为了不想多写php类的代码,加一个接口php,目的是,让android与flex都共享调用amfphp文件
android端:
///////////////////////////////////////////////////////////////////////////////
///传参 写入数据库
//////////////////////////////////////////////////////////////////////////////
private void postJsonArray() {
try {
//测试数据
ArrayList list = new ArrayList();
list.add(new KeyValuePair("diding", "男", "13890540183"));
list.add(new KeyValuePair("diding2", "男", "13890540183"));
list.add(new KeyValuePair("diding3", "男", "13890540183"));
JSONArray jsonArr = new JSONArray();
for (KeyValuePair p : list) {
JSONObject tempJson = new JSONObject();
tempJson.put("userName", p.userName);
tempJson.put("sex", p.sex);
tempJson.put("phone", p.phone);
jsonArr.put(tempJson);
}
JSONObject json = new JSONObject();
//执行插入数据
json.put("flag", "insertUser");
json.put("data", jsonArr);
String jsonStr = json.toString();
Log.e("===", "==" + jsonStr);
HttpUtils.postJson(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("TAG", "NetConnect error!");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseStr = response.toString();
String responseBodyStr = response.body().string();
try {
JSONObject jsonData = new JSONObject(responseBodyStr);
String resultStr = jsonData.getString("status");
//得到服务器返回的具体内容
if (resultStr.equals("success"))
{
Log.e("写入数据", "写入数据成功");
} else {
Log.e("写入数据", "写入数据失败");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, jsonStr);
} catch (
JSONException e)
{
e.printStackTrace();
}
}
///////////////////////////////////////////////////////////////////////////////
///用 传参 读数据库
//////////////////////////////////////////////////////////////////////////////
private void getJsonArray() {
try {
JSONObject jsonData = new JSONObject();
jsonData.put("gradeName", "初2019级");
JSONObject json = new JSONObject();
//执行读mysql数据,php端BehaviorAdminPage 主供flex调用amfphp
json.put("flag", "readTermInfo");
json.put("data", jsonData);
String jsonStr = json.toString();
Log.e("===", "==" + jsonStr);
HttpUtils.postJson(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("TAG", "NetConnect error!");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseStr = response.toString();
String responseBodyStr = response.body().string();
try {
JSONObject jsonData = new JSONObject(responseBodyStr);
String resultStr = jsonData.getString("status");
//得到服务器返回的具体内容
if (resultStr.equals("success"))
{
String resultData = jsonData.getString("data");
Log.e("成功", "数据是=="+resultData);
} else {
//注册失败
Log.e("写入数据", "写入数据失败");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, jsonStr);
} catch (
JSONException e)
{
e.printStackTrace();
}
}
php
{'flag'};
//android客户端传过来的数data
$android_command_data = $phpJson->{'data'};
$data = array();
$response = array();
$bol;
switch ($android_command_flag) {
case "readTermInfo":
$behavior = new BehaviorAdminPage();
//不再使用json_decode转换,因为接收过来已转换
$obj = $android_command_data;
$result = $behavior->ReadGradeTermNameInfo($obj->{'gradeName'});
//解析数据,因为类方法返回的数据是未处理的
for ($i = 0; $i < count($result); $i++) {
$data[$i]['id'] = $result[$i]['id'];
$data[$i]['gradeName'] = $result[$i]['gradeName'];
$data[$i]['termName'] = $result[$i]['termName'];
$data[$i]['beginDate'] = $result[$i]['beginDate'];
}
if ($data != null) {
$response["status"] = "success";
//绑定返回用于解析
$response["data"] = $data;
} else {
$response["status"] = "failed";
}
break;
case "insertUser":
$jsonArr = $android_command_data;
for ($i = 0; $i < count($jsonArr); $i++) {
//$obj = json_decode($jsonArr[$i]);
//不再使用json_decode转换,因为接收过来已转换
$obj = $jsonArr[$i];
$sqlStr = "INSERT INTO pxmobile_admins_teachers_parents(userName, sex, phone) VALUES('" .
$obj->{'userName'} . "','" . $obj->{'sex'} . "', '" . $obj->{'phone'} . "')";
$result = mysql_query($sqlStr);
if ($result) {
$bol = true;
} else {
$bol = false;
}
}
if ($bol) {
$response["status"] = "success";
} else {
$response["status"] = "failed";
}
break;
}
//呈现数据
header('Content-type: application/json');
echo json_encode($response);
?>
Android与Flex (amfphp)共享 BehaviorAdminPage.php
judgeRecordIsExist($gradeName, $className);
if ($bol) {
$sql = "select * from sczzez_behavior_reg where gradeName='$gradeName' and className='$className'";
$result = mysql_query($sql);
while ($row = mysql_fetch_Array($result)) {
$ArrayOfData[] = $row;
}
return ($ArrayOfData);
} else {
$className2 = "grade";
$sql2 = "select * from sczzez_behavior_reg where gradeName='$gradeName' and className='$className2'";
$result2 = mysql_query($sql2);
while ($row2 = mysql_fetch_Array($result2)) {
$ArrayOfData2[] = $row2;
}
return ($ArrayOfData2);
}
}
/**
*
*
*
* 二级模块 . Flex . 操行条例
*
*
*
*
* *************************************************/
/**
* private
* 判断记录是否存在有,有,则返回true, 没有则返回 false
* ***********/
private function judgeRecordIsExist2($gradeName, $className, $itemCode)
{
$result = mysql_query("select gradeName,className,itemCode from sczzez_behavior_reg where gradeName='$gradeName' AND className='$className' And itemCode='$itemCode' ");
if ($result) {
if (!($row = mysql_fetch_array($result))) {
return false;
} else {
return true;
}
}
}
//写入操行规则数据
public function FullOrPartInsertBehaviorRegData($dataArr)
{
//先删除重复的提交
$checkPass = false;
$isFullInsert = $dataArr[0]['insertKind'];
$gradeName = $dataArr[0]['gradeName'];
$className = $dataArr[0]['className'];
if ($isFullInsert) {
$result2 = mysql_query("delete from sczzez_behavior_reg where gradeName='$gradeName' AND className='$className'");
$checkPass = true;
} else {
//检验是否存在教师的系统编号
for ($i = 0; $i < count($dataArr); $i++) {
$gradeName = $dataArr[$i]['gradeName'];
$className = $dataArr[$i]['className'];
$itemCode = $dataArr[$i]['itemCode'];
$bol = $this->judgeRecordIsExist2($gradeName, $className, $itemCode);
if ($bol) {
$checkPass = false;
return $checkPass;
break;
} else {
$checkPass = true;
}
}
}
$returnValue = false;
if ($checkPass) {
for ($i = 0; $i < count($dataArr); $i++) {
$gradeName = $dataArr[$i]['gradeName'];
$className = $dataArr[$i]['className'];
$itemCode = $dataArr[$i]['itemCode'];
$controlKind = $dataArr[$i]['controlKind'];
$itemName = $dataArr[$i]['itemName'];
$score = $dataArr[$i]['score'];
$addTime = date("Y-m-d H:i:s");
//执行写入
$sql = "INSERT INTO sczzez_behavior_reg ";
$sql .= "(gradeName,className,itemCode,controlKind,itemName,score,addTime)VALUES(";
$sql .= "'" . $gradeName . "',";
$sql .= "'" . $className . "',";
$sql .= "'" . $itemCode . "',";
$sql .= "'" . $controlKind . "',";
$sql .= "'" . $itemName . "',";
$sql .= "'" . $score . "',";
$sql .= "'" . $addTime . "'" . ")";
$result = mysql_query($sql);
if ($result == true) {
$returnValue = true;
} else {
return false;
break;
}
}
}
if ($returnValue == true) {
return true;
} else {
return false;
}
}
/**
*
* *********/
public function DelRecord($id)
{
$result = mysql_query("delete from sczzez_behavior_reg where id='$id'");
return $result;
}
}
?>
Flex: