C
C
carlsson2016-10-18 07:16:34
Java
carlsson, 2016-10-18 07:16:34

Android HttpURLConnection is loaded only part of the response to the request, how to download the whole?

There is a common code.
The problem is that the result I get is not complete. But only some part, that is, not even a part, but the first 4066 characters. IOException doesn't seem to be thrown

private class GetContent extends AsyncTask<Void, Void, String>{

    HttpURLConnection urlConnection = null;
    BufferedReader reader = null;
    String resultJson = "";

    @Override
    protected String doInBackground(Void... params) {

        try{
            URL url = new URL("http://www.json-generator.com/api/json/get/bSFiRdwiSq?indent=2");

            urlConnection = (HttpURLConnection)url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();

            InputStream inputStream = urlConnection.getInputStream();
            StringBuffer buffer = new StringBuffer();

            reader = new BufferedReader(new InputStreamReader(inputStream));

            String line;
            while ((line = reader.readLine()) != null){
                buffer.append(line);
            }

            resultJson = buffer.toString();

            Log.d("Result:", resultJson);

        } catch (MalformedURLException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}

Result
D/RESULT:: [  {    "guid": "fdaefcc1-6469-4e3a-9d03-722f8de768d9",     "index": 0,     "favoriteFruit": "banana",     "latitude": 52.644930000000002,     "company": "DECRATEX",     "email": "[email protected]",     "picture": "http://placehold.it/32x32",     "tags": [      "esse",       "tempor",       "fugiat",       "labore",       "eu",       "aliquip",       "fugiat"    ],     "registered": "2015-06-07T09:21:48 -06:00",     "eyeColor": "blue",     "phone": "+1 (870) 541-3015",     "address": "503 Rugby Road, Defiance, Alaska, 2923",     "friends": [      {        "id": 0,         "name": "Melton Reese"      },       {        "id": 1,         "name": "Sara Gilbert"      },       {        "id": 2,         "name": "Nicholson Weber"      }    ],     "isActive": false,     "about": "Fugiat Lorem mollit in pariatur incididunt est cupidatat veniam sit officia. Dolor dolor velit ex fugiat dolore officia enim quis in pariatur do ea. Ipsum cillum duis aliquip ut occaecat qui sint est aliqua consequat reprehenderit non. Velit pariatur aute voluptate minim in deserunt amet duis laborum laborum amet. Fugiat nostrud culpa ipsum Lorem proident pariatur aliqua fugiat culpa. Et aliquip magna exercitation ipsum aute voluptate. Dolore magna sint duis ipsum eu consectetur.\r\n",     "balance": "$3,384.03",     "name": "Jodie Collier",     "gender": "female",     "age": 27,     "greeting": "Hello, Jodie Collier! You have 3 unread messages.",     "longitude": 173.90763799999999,     "_id": "58046a786230071a551569b9"  },   {    "guid": "badaf4d6-54fa-46e3-8a6d-6b6997483b00",     "index": 1,     "favoriteFruit": "strawberry",     "latitude": 39.709885,     "company": "DRAGBOT",     "email": "[email protected]",     "picture": "http://placehold.it/32x32",     "tags": [      "elit",       "ea",       "ipsum",       "velit",       "amet",       "laborum",       "duis"    ],     "registered": "2016-06-01T05:06:26 -06:00",     "eyeColor": "green",     "phone": "+1 (911) 423-3011",     "address": "248 Forbell Street, Mathews, Vermont, 4994",     "friends": [      {        "id": 0,         "name": "Liza Day"      },       {        "id": 1,         "name": "Catherine Palmer"      },       {        "id": 2,         "name": "Beryl Conway"      }    ],     "isActive": false,     "about": "Proident id fugiat pariatur et incididunt commodo est irure in duis ullamco veniam est magna. Id enim qui commodo exercitation labore adipisicing excepteur adipisicing dolor veniam. Qui quis minim commodo mollit est sit.\r\n",     "balance": "$1,375.60",     "name": "Alston Mays",     "gender": "male",     "age": 33,     "greeting": "Hello, Alston Mays! You have 10 unread messages.",     "longitude": -67.445130000000006,     "_id": "58046a78a50c13da27ab0d11"  },   {    "guid": "e328c11c-f948-42a6-aede-179ec44a7873",     "index": 2,     "favoriteFruit": "apple",     "latitude": 17.807843999999999,     "company": "INSOURCE",     "email": "[email protected]",     "picture": "http://placehold.it/32x32",     "tags": [      "et",       "anim",       "dolor",       "magna",       "quis",       "occaecat",       "dolor"    ],     "registered": "2015-09-11T03:36:24 -06:00",     "eyeColor": "blue",     "phone": "+1 (865) 558-2301",     "address": "189 Cranberry Street, Riverton, Arkansas, 5369",     "friends": [      {        "id": 0,         "name": "Whitley Howe"      },       {        "id": 1,         "name": "Roseann Perez"      },       {        "id": 2,         "name": "Sasha Richard"      }    ],     "isActive": false,     "about": "Consequat adipisicing ut dolor in. Id enim consectetur qui incididunt non amet laboris. Veniam amet dolore proident est proident dolor sunt nulla est quis veniam commodo anim. Ut exercitation excepteur enim quis et ea veniam non laboris duis. Dolore cillum commodo dolore reprehenderit aliqua labore.\r\n",     "balance": "$3,678.92",     "name": "Cassandra Mcintyre",     "gender": "female",     "age": 29,     "greeting": "Hello, Cassandra Mcintyre! You have 8 unread messages.",     "longitude": 93.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mitaichik, 2016-10-18
@mitaichik

Use okhttp/retrofit

R
Ruslan Lopatin, 2016-10-18
@lorus

I would venture to suggest that the entry in the log was simply cut off.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question