PHP开发APP接口
一.php后台给APP android 封装传递数据
1. json方式封装接口
<?php
class Response{
/*
*按json方式输出通信数据
* integer $code 状态码
* string $message 提示信息
* array $data 数据
*/
public static function json($code,$message='',$data){
if(!is_numeric($code)){
return '';
}
$result=array(
'code'=>$code,
'message'=>$message,
'data'=>$data
);
//$result=iconv('GBK' , 'UTF-8',$result); 将utf-8转换为gbk
echo json_encode($result);
exit();
}
}
2. xml方式封装接口
/*
*按xml方式输出通信数据
* integer $code 状态码
* string $message 提示信息
*@param array $data 数据
*/
public static function xmlEncode($code,$message,$data=array()){
if(!is_numeric($code)){
return '';
}
$result=array(
'code'=>$code,
'message'=>$message,
'data'=>$data
);
header("Content-Type:text/xml");
$xml="<?xml version='1.0' encoding='UTF-8'?>\n";
$xml.="<root>\n";
$xml.=self::xmlToEncode($result);
$xml.="</root>";
echo $xml;
}
//将数组拼装成xml
public static function xmlToEncode($data){
// 注:xml节点不能为数字
$xml=$atte="";
foreach ($data as $key => $value) {
if(is_numeric($key)){
$atte="id='{$key}'";
$key="item";
}
$xml.="<{$key}{$atte}>";
$xml.=is_array($value) ? self::xmlToEncode($value) : $value;
$xml.="</{$key}>\n";
}
return $xml;
}
3. 综合方式输出通信数据
/*
*综合方式输出通信数据
*@param integer $code 状态码
*@param string $message 提示信息
*@param array $data 数据
*@param array $type 数据类型 默认json
*/
public static function show($code,$message='',$data=array(),$type='json'){
if(!is_numeric($code)){
return '';
}
$result=array(
'code'=>$code,
'message'=>$message,
'data'=>$data
);
if($type=='json'){
self::json($code,$message,$data);
exit;
}elseif($type=='array'){ //调试模式
var_dump($result);
}elseif($type=='xml'){
self::xmlEncode($code,$message,$data);
}else{
}
}
二.Android给PHP后台传递数据(图片)
Android端
1. //HTTP上传图片
2. RequestParams params = new RequestParams();
3. try {
4. //将压缩后的bitmap保存为图片文件
5. String saveImgPath=getSD_Path()+"/saveimg.png";
6. File saveimg=new File(saveImgPath);
7. FileOutputStream fos = new FileOutputStream(saveimg);
8. bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
9. fos.flush();
10. fos.close();
11. //上传压缩后的文件,大约100k左右
12. File uploadImg=new File(saveImgPath);
13. <span style="color:#ff0000;">params.put("attach", uploadImg);</span>
14. } catch (FileNotFoundException e) {
15. e.printStackTrace();
16. } catch (IOException e) {
17. e.printStackTrace();
18. }
19. //上传地址
20. String url=URLConfigs.UploadHeadImage_ukey+myprefs.Ukey().get();
21. <span style="color:#ff0000;">String url="http://192.168.0.8/upload.php";</span>
22. LogUtil.e(TAG, "upload img url :"+url);
23. AsyncHttpUtil.post_loading(context,url, params, new MyTextHttpResponseHandler() {
24. @Override
25. public void onSuccess(int status, Header[] arg1, String json) {
26. super.onSuccess(status, arg1, json);
27. LogUtil.e(TAG, "上传图片 json :"+json);
28. RespondBaseEntity entity=GsonUtil.GetFromJson(json, RespondBaseEntity.class);
29. if(entity.isStatus()){
30. //上传成功,设置图片
31. face.setImageBitmap(bmp);
32. ToastUtils.show(context, "上传成功");
33. }else{
34. ToastUtils.show(context, json);
35. }
36.
37. myprefs.position().put(0);
38. }
2.PHP端接收
<?php
$base_path = "./upload/"; //存放目录
if(!is_dir($base_path)){
mkdir($base_path,0777,true);
}
$target_path = $base_path . basename ( $_FILES ['attach'] ['name'] );
if (move_uploaded_file ( $_FILES ['attach'] ['tmp_name'], $target_path )) {
$array = array (
"status" => true,
"msg" => $_FILES ['attach'] ['name']
);
echo json_encode ( $array );
} else {
$array = array (
"status" => false,
"msg" => "There was an error uploading the file, please try again!" . $_FILES ['attach'] ['error']
);
echo json_encode ( $array );
}
?>