1

So I have a piece of code where I am making a call to a server to get a large JSON file (about 2MB) and am parsing it using Jackson JSON Parser. The code runs fine, but the problem is I sometimes get an IOException "unexpected end of stream." So I'm guessing I'm losing my connection on my emulator during the streaming. Is there anyway that I can pause the stream and resume it? Or reconnect then resume the stream? Thanks.

private void createDatabase(String downloadUrl)
{
    editor = sharedPref.edit();

    String fieldName = "";
    long id = 0;
    String setNumber = "";
    String cardName = "";
    String type = "";
    String attribute = "";
    String level = "";
    String category = "";
    String attack = "";
    String defense = "";
    String description = "";

    try
    {
        if (isOnline())
        {
            url = new URL(downloadUrl);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            jsonfactory = new JsonFactory();
            jsonParser = jsonfactory.createParser(connection.getInputStream());

            while (jsonParser.nextToken() != JsonToken.END_ARRAY)
            {
                if (jsonParser.getCurrentToken() == JsonToken.FIELD_NAME)
                {
                    fieldName = jsonParser.getCurrentName();

                    // if (fieldName.equals("id"))
                    // {
                    // jsonParser.nextToken();
                    // id = jsonParser.getIntValue();
                    // }

                    if (fieldName.equals("Set_Number"))
                    {
                        jsonParser.nextToken();
                        setNumber = jsonParser.getText();
                    }

                    else if (fieldName.equals("Card_Name"))
                    {
                        jsonParser.nextToken();
                        cardName = jsonParser.getText();
                    }

                    else if (fieldName.equals("Type"))
                    {
                        jsonParser.nextToken();
                        type = jsonParser.getText();
                    }

                    else if (fieldName.equals("Attribute"))
                    {
                        jsonParser.nextToken();
                        attribute = jsonParser.getText();
                    }

                    else if (fieldName.equals("Level"))
                    {
                        jsonParser.nextToken();
                        level = jsonParser.getText();
                    }

                    else if (fieldName.equals("Category"))
                    {
                        jsonParser.nextToken();
                        category = jsonParser.getText();
                    }

                    else if (fieldName.equals("Attack"))
                    {
                        jsonParser.nextToken();
                        attack = jsonParser.getText();
                    }

                    else if (fieldName.equals("Defense"))
                    {
                        jsonParser.nextToken();
                        defense = jsonParser.getText();
                    }

                    else if (fieldName.equals("Description"))
                    {
                        jsonParser.nextToken();
                        description = jsonParser.getText();
                    }
                }

                else if (jsonParser.getCurrentToken() == JsonToken.END_OBJECT)
                {
                    id = db.insert(setNumber, cardName, type, attribute, level, category, attack, defense, description);
                    editor.putLong("lastId", id);
                    editor.commit();
                }
            }
        }
    }

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

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

    finally
    {
        if (connection != null)
            connection.disconnect();

        if (jsonParser != null)
        {
            try
            {
                jsonParser.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    Log.d("lastID", "" + sharedPref.getLong("lastId", -1));
    if (sharedPref.getLong("lastId", -1) == 5980)
    {
        editor.putBoolean("databaseExists", true);
        editor.commit();
    }
}
Sphig316
  • 216
  • 2
  • 13

2 Answers2

1

So I figured out the solution. It looks like the way I was connecting to my server was bad. I found this piece of code to get HTTP GET responses and created an InputStream from it and it works. Good luck to anyone else trying to do the same thing.

        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(downloadUrl);
        HttpResponse response = httpclient.execute(httpGet);
        HttpEntity entity = response.
        InputStream is = entity.getContent();

        jsonfactory = new JsonFactory();
        jsonParser = jsonfactory.createParser(is);
Sphig316
  • 216
  • 2
  • 13
0

Details about pausing/resuming file download are mentioned here.

Pause/Resume http connection download

Note : If you think it is the problem of emulator connection with internet then you can run your apk on your mobile also by enabling developer options under settings.

Community
  • 1
  • 1
Hrushi
  • 153
  • 3
  • 13
  • Sorry, I didn't mean to put download. I'm actually not downloading anything. I'm doing a live stream where I read a part of the JSON then save it in a variable then go to the next part until I get to the end of the JSON Array – Sphig316 Aug 20 '14 at 18:25
  • can you please post exception and code snippet that you're using. It would be helpful to understand this issue and resolve it. – Hrushi Aug 20 '14 at 18:58
  • I just put my method that I'm using to stream the JSON – Sphig316 Aug 20 '14 at 19:34
  • Not so sure what could be the problem here. Code looks fine. Also as per documentation , The input and output streams returned by this class are not buffered. If you're reading chunks of data you should wrap the streams with BufferedInputStream. Can you please try it and check the result.Buffering might be helpful to avoid this issue. Documentation Link : http://developer.android.com/reference/java/net/HttpURLConnection.html – Hrushi Aug 21 '14 at 15:07