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.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!