php 做安卓 服务器 怎么编写代码
php 只能做服务端的数据处理,然后将数据用接口的方式,以json的数据形式给安卓客户端程序调用。
testphp
<!--php
class Response {
const JSON = json;
/
按综合方式输出通信数据
@param integer $code 状态码
@param string $message 提示信息
@param array $data 数据
@param string $type 数据类型
return string
/
public static function show($code, $message = '', $data = array(), $type = self::JSON) {
if(!is_numeric($code)) {
return '';
}
$type = isset($_GET['format']) $_GET['format'] : self::JSON;
$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);
exit;
} else {
// TODO
}
}
/
按json方式输出通信数据
@param integer $code 状态码
@param string $message 提示信息
@param array $data 数据
return string
/
public static function json($code, $message = '', $data = array()) {
if(!is_numeric($code)) {
return '';
}
$result = array(
'code' => $code,
'message' => $message,
'data' => $data
);
echo json_encode($result);
exit;
}
/
按xml方式输出通信数据
@param integer $code 状态码
@param string $message 提示信息
@param array $data 数据
return string
/
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='10' encoding='UTF-8'-->
;
$xml = <root>
;
$xml = self::xmlToEncode($result);
$xml = </root>;
echo $xml;
}
public static function xmlToEncode($data) {
$xml = $attr = ;
foreach($data as $key => $value) {
if(is_numeric($key)) {
$attr = id='{$key}';
$key = item;
}
$xml = <{$key}{$attr}>;
$xml = is_array($value) self::xmlToEncode($value) : $value;
$xml = <!--{$key}-->
;
}
return $xml;
}
}
<!--php
require_once('/testphp');
$data=array(
'id'=-->1,
'name'=>david
);
Response::show(200,'success',$data);
from bottle import run,route,request,response
from urllibrequest import urlopen
@route('<url:re:>')
def get_method(url):
data=urlopen(url)
return dataread()
run(host='0000',port=3456,debug=True)
我简单写一个,使用bottle框架,注意不要该ie的代理,使用别的浏览器如firefox代理到
127001:3456
1、安装ftp服务端,方便你从客户端上传文件。linux上一般安装vsftp
VSFTP的软件安装包是: vsftpd-222-12el6_51i686rpm
查询:#rpm -q vsftpd
安装:#rpm -ivh vsftpd-222-12el6_51i686rpm或者直接yum install vsftpd
启动、停止及重启VSFTP的命令: #service vsftpd start/stop/restart
配置文件:
/etc/vsftpd/vsftpdconf //主配置文件
/etc/vsftpdftpusers //被禁止登录FTP的用户文件
/etc/vsftpduser_list //允许登录FTP的用户文件
a、将登录后的用户限制在本地家目录下:
#vi /etc/vsftpd/vsftpdconf
chroot_list_enable=YES
chroot_list_file=/etc/vsftpdchroot_list
新建受限用户的列表文件并加入受限用户名
#vi /etc/vsftpdchroot_list
raykeso
重启ftp服务
#service vsftpd restart
2、安装apache,开启目录索引,这样你就可以浏览和下载文件。
apache配置文件
<Directory "I:/build/dist"> #设置浏览文件的目录
Options Indexes #开启目录列表索引模式Order allow,deny
Allow from all
</Directory>
下面是我自己写的一个读取并显示txt文件的demo,希望对您有帮助。
public class Client {
public static void main(String[] args) {
ClientFrame f = new ClientFrame();
}
}
import javaawtBorderLayout;
import javaawtContainer;
import javaawtDimension;
import javaawtGridLayout;
import javaawtToolkit;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaioDataInputStream;
import javaioDataOutputStream;
import javaioIOException;
import javanetSocket;
import javanetUnknownHostException;
import javautilVector;
import javaxswingBorderFactory;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJList;
import javaxswingJScrollPane;
import javaxswingJTextArea;
import javaxswingeventListSelectionEvent;
import javaxswingeventListSelectionListener;
public class ClientFrame extends JFrame implements ActionListener, ListSelectionListener{
private JList list = null;
private JButton sbtn = null;
private JButton cbtn = null;
private Vector v = null;
private JTextArea txt = null;
private Container control = null;
private Container btn = null;
private Socket client = null;
private DataInputStream reader = null;
private DataOutputStream writer = null;
public ClientFrame(){
thislist = new JList();
thislistsetBorder(BorderFactorycreateTitledBorder("文件列表"));
thislistaddListSelectionListener(this);
thissbtn = new JButton("显示");
thissbtnaddActionListener(this);
thiscbtn = new JButton("清除");
thiscbtnaddActionListener(this);
thiscontrol = new Container();
thiscontrolsetPreferredSize(new Dimension(150, 400));
thiscontrolsetLayout(new BorderLayout());
thiscontroladd(new JScrollPane(thislist),BorderLayoutCENTER);
thisbtn = new Container();
thisbtnsetLayout(new GridLayout(1,2));
btnadd(sbtn);
btnadd(cbtn);
thiscontroladd(thisbtn,BorderLayoutSOUTH);
thistxt = new JTextArea();
thistxtsetEditable(false);
thistxtsetSize(350, 400);
thissetTitle("客户端");
thissetSize(500, 400);
thissetVisible(true);
Dimension displaySize = ToolkitgetDefaultToolkit()getScreenSize();
thissetLocation((displaySizewidth - thisgetWidth()) / 2, (displaySizeheight - thisgetHeight()) / 2);
thissetLayout(new BorderLayout());
thisadd(thiscontrol,BorderLayoutWEST);
thisadd(new JScrollPane(thistxt),BorderLayoutCENTER);
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
try {
//thisclient = new Socket("1921683234",6666);
thisclient = new Socket("1921681100",6666);
thisreader = new DataInputStream(clientgetInputStream());
thiswriter = new DataOutputStream(clientgetOutputStream());
} catch (UnknownHostException e) {
eprintStackTrace();
} catch (IOException e) {
eprintStackTrace();
}
}
public void actionPerformed(ActionEvent event){
if(eventgetSource() == sbtn){
if(v == null){
v = new Vector();
}
else{
vclear();
}
try {
writerwriteUTF("getfilelist");
writerflush();
String t = readerreadUTF();
while( t != null && !tequals("")){
vadd(t);
t = readerreadUTF();
}
} catch (UnknownHostException e) {
eprintStackTrace();
} catch (IOException e) {
eprintStackTrace();
}
thislistsetListData(v);
}
if(eventgetSource() == cbtn){
thistxtsetText("");
}
}
public void valueChanged(ListSelectionEvent e) {
int i = thislistgetSelectedIndex();
if (! thislistgetValueIsAdjusting() && i != -1) {
try {
writerwriteUTF("getfilecontent_" + i);
writerflush();
String tmp = readerreadUTF();
thistxtsetText(tmp);
} catch (IOException e1) {
e1printStackTrace();
}
}
}
}
import javaioDataInputStream;
import javaioDataOutputStream;
import javaioFile;
import javaioFileInputStream;
import javaioIOException;
import javaioInputStreamReader;
import javaioReader;
import javanetServerSocket;
import javanetSocket;
import javautilArrayList;
import javautilIterator;
public class Server {
static ArrayList<File> fileArray = new ArrayList<File>();
public static void main(String args[]) {
ServerSocket server = null;
Socket client = null;
String cmd = "";
try {
server = new ServerSocket(6666);
client = serveraccept();
DataInputStream reader = new DataInputStream(clientgetInputStream());
DataOutputStream writer = new DataOutputStream(clientgetOutputStream());
while(true){
cmd = readerreadUTF();
Systemoutprintln(cmd);
if(cmdequals("getfilelist")){
fileArrayclear();
//fileArray = getFile(new File("D:/tmp"));
fileArray = getFile(new File("D:/学习/教程/学习笔记"));
String fn = "";
for(int k = 0; k < fileArraysize(); k ++){
fn = fileArrayget(k)getName();
writerwriteUTF(fn);
writerflush();
}
writerwriteUTF("");
}
if(cmdstartsWith("getfilecontent_")){
int i = IntegerparseInt(cmdsplit("_")[1]);
File f = fileArrayget(i);
Reader in = new InputStreamReader(new FileInputStream(f));
int tempbyte;
String str = "";
while ((tempbyte = inread()) != -1) {
str += (char)tempbyte;
//Systemoutprintln(str);
}
inclose();
writerwriteUTF(str);
}
}
} catch (IOException e) {
eprintStackTrace();
}
}
private static ArrayList<File> getFile(File f) {
File[] ff = flistFiles();
for (File child : ff) {
if (childisDirectory()) {
getFile(child);
} else {
fileArrayadd(child);
}
}
return fileArray;
}
}
0条评论