Wutipong Wongsakuldej
Wutipong Wongsakuldej

Reputation: 499

OkHttp/Android does not returns full results

I have the following code, which expected to return the whole html file content loaded from http://developer.android.com .

(code is in Xtend but I think it doesn't matter).

var html = try {
        var client = new OkHttpClient()
        var request = new Request.Builder()
                        .url("http://developer.android.com/")
                        .build()
        var response = client.newCall(request).execute()

        response.body.string
    } catch(IOException err) {
        e("HTML error", err.toString)
        return
    }

    Log.d("HTML", html)

The output got cut off after a few lines of html.

<!DOCTYPE html>
<html lang="en">
<head>




<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
<meta content="IE=edge" http-equiv="X-UA-Compatible">
<meta name="description" content="The official site for Android developers. Provides the Android SDK and documentation for app developers and designers.">
<meta name="xsrf_token" content="A3XCSylWU7AR4OhsL_yhMWlzbNcbh9Hk3Csw0HAUa5E6MTQ2NDQyMTgwNzE1MDQ0MA" />

<link rel="alternate" href="http://developer.android.com/index.html" hreflang="en">
<link rel="alternate" href="http://developer.android.com/index.html?hl=es" hreflang="es">
<link rel="alternate" href="http://developer.android.com/index.html?hl=id" hreflang="id">
<link rel="alternate" href="http://developer.android.com/index.html?hl=ja" hreflang="ja">
<link rel="alternate" href="http://developer.android.com/index.html?hl=ko" hreflang="ko">
<link rel="alternate" href="http://developer.android.com/index.html?hl=pt-br" hreflang="pt-br">
<link rel="alternate" href="http://developer.android.com/index.html?hl=ru" hreflang="ru">
<link rel="alternate" href="http://developer.android.com/index.html?hl=vi" hreflang="vi">
<link rel="alternate" href="http://developer.android.com/index.html?hl=zh-cn" hreflang="zh-cn">
<link rel="alternate" href="http://developer.android.com/index.html?hl=zh-tw" hreflang="zh-tw">

<title>

  Android Developers

</title>

<!-- STYLESHEETS -->
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Roboto+Condensed">
<link rel="stylesheet"
    href="//fonts.googleapis.com/css?family=Roboto:light,regular,medium,thin,italic,mediumitalic,bold"
    title="roboto">





<link href="/static/css/default.css?v=2016052807" rel="stylesheet" type="text/css">
<!-- JAVASCRIPT -->
<script src="//www.google.com/jsapi" type="text/javascript"></script>
<script src="https://developer.android.com/_static/23ecca3dc9/js/android_3p-bundle.js" type="text/javascript"></script>


<script type="text/javascript">
  var toRoot = '/';
  var metaTags = ""; 
  var devsite = true;
  var useUpdatedTemplates = true;
  var devsiteLang = 'en';
  var pageType = 'none';
  var ANDROID_LANGUAGES = [


      'en','es','in','ja','ko','pt-br','ru','vi','zh-cn','zh-tw'

  ];
</script>
<script src="/static/js/docs.js?v=2016052807" type="text/javascript"></script>


<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  ga('create', 'UA-5831155-1', 'android.com');
  ga('create', 'UA-49880327-2', 'android.com', {'name': 'universal'});  // New tracker);
  ga('send', 'pageview');
  ga('universal.send', 'pageview'); // Send page view for new tracker.
</script>


</head>

<body class="gc-documentation none None full-width" itemscope itemtype="http://schema.org/Article">
  <a href="#top" id="skip-to-main">Skip to content</a>
  <header id="header-wrapper">
    <div class="dac-header" id="header">
      <div class="dac-header-inner">
        <a class="dac-nav-toggle" data-dac-toggle-nav href="" title="Open navigation">
          <span class="dac-nav-hamburger">
            <span class="dac-nav-hamburger-top"></span>
            <span class="dac-nav-hamburger-mid"></span>
            <span class="dac-nav-hamburger-bot"></span>
          </span>
        </a>

        <a class="dac-header-logo" href="https://developer.android.com/index.html">
          <img class="dac-header-logo-image" src="https://developer.android.com/static/images/android_logo.png"
              srcset="https://developer.android.com/static/images/android_logo_2x.png 2x"
              width="32" height="36" alt="Android"> Developers</a>
        <ul class="dac-header-tabs">
          <li>
            <a class="dac-header

I have tried using UrlConnection as well but the result is not different. This is tested on the actual device (Samsung Galaxy Note 10.1 running unofficial Cyanogen Mod) and the AVD (Atom x64 with Android 6.0).

I have also tried with a handful of url and the results is quite the same (the response get cut off somewhere).

I don't really know what should I do next. Can you please suggest ?

Upvotes: 3

Views: 1201

Answers (3)

Wutipong Wongsakuldej
Wutipong Wongsakuldej

Reputation: 499

Apologizes to every participant. It seems like the library/platform works as expected. The problem probably lies in the DDMS.

I've tried reading the whole html and then get the last few characters display on the DDMS (using Log class) and turns out it's the closing tag of html </html>. I'm pretty sure the results return as a whole.

But if I put the whole string into log functions then it get chopped off around 4000th characters. Probably the adb buffer limits or something.

Upvotes: 0

nexus5x
nexus5x

Reputation: 467

Try this:

public static String readResponseBody(Response response) throws IOException {
    BufferedReader reader = new BufferedReader(response.body().charStream());;
    StringBuilder stringBuilder = new StringBuilder();
    while (true) {
        String line = reader.readLine();
        if (line == null)
            break;
        stringBuilder.append(line);
    }
    return stringBuilder.toString();
}

Upvotes: 0

Stepan Maksymov
Stepan Maksymov

Reputation: 2606

some of data is loaded with javaScript after page is actually loaded in browser ) download javaScripts and analyse them )

Upvotes: 1

Related Questions