Best way to evaluate connection speed

后端 未结 4 1704
北荒
北荒 2021-01-31 16:10

I\'m developing an app which needs to get music file by streaming for playing live.

In the request song api I can specify the bandwith (eg: 50kbps, 100kbps, 300, 600 or

4条回答
  •  北海茫月
    2021-01-31 16:35

    protected String doInBackground(String... urls) {
        String response = "";
    
        startTime = System.currentTimeMillis();
        for (String url : urls) {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            try {
    
                HttpResponse execute = client.execute(httpGet);
                InputStream content = execute.getEntity().getContent();
    
    
                BufferedReader buffer = new BufferedReader(
                        new InputStreamReader(content));
                String s = "";
                while ((s = buffer.readLine()) != null) {
                    response += s;
                }
                endTime = System.currentTimeMillis();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return response;
    }
    
    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
    
    
        long dataSize = result.length() / 1024;
        takenTime = endTime - startTime;
        long s = takenTime / 1000;
        double speed = dataSize / s;
    
        Toast.makeText(context, "" + s + "kbps", Toast.LENGTH_SHORT).show();
    }
    

提交回复
热议问题