duannei1477 2018-11-12 23:27
浏览 966

让android studio从web获取数据并添加到url

I own a live tv app on android, but it was only made to play normal streams. However, my streams have token authentication in order to play so the stream must get the token from my server and add it to the end of the url then play. How can I do this on android studio? I basically am trying to get the app to get the token from my webpage where it is generated and add it to the end of the stream url then play.

I would appreciate it if someone could point me in the right direction. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dt97868 2018-11-13 00:18
    关注

    To do that you need to provide your access-token from a wen service, thene add the token to the end of the url, thene usethe new URL.

    here an eample of a code (PHP) that generates a random access token thene provide it in the address " path-to-script/gettoken.php "

    <?php
         // the content of gettoken.php
    
        // a funtion to generate random strings : they will serve as your tokens 
            function generateRandomString($length = 10) {
                $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                $charactersLength = strlen($characters);
                $randomString = '';
                for ($i = 0; $i < $length; $i++) {
                    $randomString .= $characters[rand(0, $charactersLength - 1)];
                }
                return $randomString;
            }
    
    // thene we will send the tokenwhee we recieve a GETrequestin the url of the script.
     echo generateRandomString(10)
    ?>
    

    In the Java Code you will get the token by the following lines of code :

    // a function to send a get request 
    public String sendGetRequest(String requestURL) {
            StringBuilder sb = new StringBuilder();
            try {
                URL url = new URL(requestURL);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
    
                String s;
                while ((s = bufferedReader.readLine()) != null) {
                    sb.append(s + "
    ");
                }
            } catch (Exception e) {
            }
            return sb.toString();
        }
    
    // gettig a token
    String URL = "The url of the php script" // replace itwithe  correct URL
    String token=sendGetRequest(URL)
    

    By this you will be able to retrieve the token in your android app thene useitin the code.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向