Android OTA应用访问Thinkphp5 服务器

从学习服务器端Thinkphp5 到现在已有半个月,目前为止,尝试做的OTA后台服务器已经可以运行,手机端的OTA应用也可以通过服务器下载升级包并完成升级。

这期间还是能学习到不少东西的,比如php、html、js、数据库、网络等,实践证明多动手才能发现问题,才能发现不足。

多学习才能发现自己的无知,才体会到知识的浩瀚无穷和力量。


服务器做好了,如何访问服务器呢?

1. 可以通过Html网页访问,提交数据。比如浏览器,html 是运行在客户端的,html里的数据是服务器返回的。

http://localhost/public/index.php/admin/login/index.html


2. 也可以通过下面这样访问,直接访问控制器Device的checkNewVersion函数,后面带参数,红色是参数名,后面跟参数值。一般这个的形式称作get方式

http://localhost/public/index.php/home/Device/checkNewVersion/project_name/U501/version_name/version1111/device_number/sn11111

这种方式可以在浏览器里直接输入,也可以在应用里代码提交。


3. 因为get方式提交,参数可见,所以大多时候需要post方式。

    

   如下,要实现手机是否有新版本软件需要升级,访问home模块的Device控制器checkNewVersion函数

    private static final String serverUrl = "http://192.168.23.1/public/index.php/home/Device/checkNewVersion";

  •    手机应用端提交请求
  • 	private void checkNewVersionInner() throws Exception {
    		String getData = null;
    		boolean hasNewVersion = false;
    		Map<String, String> parames = new HashMap<String, String>();
    		parames.put("project_name", MODEL);
    		parames.put("version_name", RELEASE);
    		parames.put("device_number", "123456789012345");
    		getData = HttpUtil.sendPostRequest(serverUrl, parames, "UTF-8");
    


  •     public static String sendPostRequest(String path,   
                Map<String, String> params, String enc) throws Exception{  
              
            // title=dsfdsf&timelength=23&method=save  
            StringBuilder sb = new StringBuilder();
            if(params!=null && !params.isEmpty()){  
    
                for(Map.Entry<String, String> entry : params.entrySet()){  
    				sb.append(entry.getKey()).append('=')
    						.append(URLEncoder.encode(entry.getValue(), enc))
    						.append('&');
                }  
                sb.deleteCharAt(sb.length()-1);
            }
            System.out.println("post: send_data:" + sb.toString());
            
            byte[] entitydata = sb.toString().getBytes();
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("POST");
            conn.setConnectTimeout(50 * 1000);
    
            conn.setDoOutput(true);
    
            //Content-Type: application/x-www-form-urlencoded  
            //Content-Length: 38  
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", String.valueOf(entitydata.length));
     
    
            
             OutputStream outStream = conn.getOutputStream();
            outStream.write(entitydata);
            outStream.flush();
            outStream.close();
            if(conn.getResponseCode()==200){  
    


  • 服务器端处理请求,同时处理get和post方式。
  •     public function checkNewVersion($project_name='',$version_name='',$device_number='')
        {
            define('IS_POST',       Request::instance()->isPost());
            $responed = $this->create_responed();
            if(IS_POST){
                $where = array(
                'project_name' => $_POST['project_name'],
                'version_name' => $_POST['version_name']
                );
                $device_number = $_POST['device_number'];
            }else{
             $where = array(
                'project_name' => $project_name,
                'version_name' => $version_name
                );
    
                
            }
            Log::record('check_new_version $where ' . var_export($where, true), 'error');
            $Project_version = model('admin/Project')->join("darling_version","darling_version.project_id = darling_project.project_id")
            ->where($where)->find();
            
            if(!empty($Project_version)){
                Log::record('check_new_version Project_version ' . var_export($Project_version, true), 'error');
                
                $package = model('admin/Package')
                ->where("project_id=".$Project_version['project_id']." and version_id=".$Project_version['version_id']." and status>1")
                ->order('create_time desc')
                ->find();          
                if(!empty($package)){
                    Log::record('check_new_version path ' . var_export($package, true), 'error');
                    $responed = array(
                    'NeedUpdate' => '1',
                    'Path' => $_SERVER['HTTP_HOST'].DS.$package['package_path'],
                    'CheckSum'=>$package['package_md5'],
                    'Size'=>$package['package_size'],
                    'TargetVersion'=>$package['target_version']
                    );
                    $path = str_replace("\\",'/',$package['package_path']);
                      
                    $responed = $this->create_responed('1',"http://".$_SERVER['HTTP_HOST'].'/'.$path,$package['package_md5'],$package['package_size'],$package['target_version']);        
                }
            }  
    
            //dump($responed);
            return $responed;
        }
    //  Create XML Item
        public function create_responed($NeedUpdate='0', $Path='', $CheckSum='', $Size='', $TargetVersion='' ) {
     	$item = "<item>\n";
     	$item .= "    <NeedUpdate>" .$NeedUpdate. "</NeedUpdate>\n";
     	$item .= "    <Path>" . $Path . "</Path>\n";
     	$item .= "    <CheckSum>" . $CheckSum . "</CheckSum>\n";
     	$item .= "    <Size>" . $Size . "</Size>\n";
     	$item .= "    <TargetVersion>" . $TargetVersion . "</TargetVersion>\n";
     	$item .= "</item>\n";
     	return $item;
     	}  

           注意,需要手机和服务器在同一个网段,才能通信。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值