0

I'm having a problem with an HTTP request from my Android App.
I'm trying to get this JSON String :

{
    "produto": [
        {
            "pro_id": 2,
            "pro_nome": "MELAO TIPO12"
        },
        {
            "pro_id": 3,
            "pro_nome": "MAMAO PAPAIA TIPO 15"
        },
        {
            "pro_id": 4,
            "pro_nome": "MELANCIA GRANDE"
        },
        {
            "pro_id": 11,
            "pro_nome": "MARACUJA SUPER"
        },
        {
            "pro_id": 17,
            "pro_nome": "MORANGO"
        },
        {
            "pro_id": 18,
            "pro_nome": "MANGA THOMY 12"
        },
        {
            "pro_id": 19,
            "pro_nome": "ABACAXI PEROLA TIPO 5"
        },
        {
            "pro_id": 20,
            "pro_nome": "ABACAXI PEROLA TIPO 15"
        },
        {
            "pro_id": 21,
            "pro_nome": "ABACAXI HAWAI 1IPO 4"
        },
        {
            "pro_id": 22,
            "pro_nome": "ABACAXI HAWAI TIPO 15"
        },
        {
            "pro_id": 23,
            "pro_nome": "ABACAXI HAWAI TIPO 5"
        },
        {
            "pro_id": 29,
            "pro_nome": "ABACAXI PEROLA TIPO 4"
        },
        {
            "pro_id": 30,
            "pro_nome": "ABACAXI PEROLA TIPO 5 PEQUENO"
        },
        {
            "pro_id": 36,
            "pro_nome": "ABACAXI HAWAI TIPO 10"
        },
        {
            "pro_id": 41,
            "pro_nome": "ABACATE"
        },
        {
            "pro_id": 44,
            "pro_nome": "AMEIXA IMPORT"
        },
        {
            "pro_id": 45,
            "pro_nome": "AMENDOIM TORRADO COM CASCA"
        },
        {
            "pro_id": 51,
            "pro_nome": "MARACUJA 3A"
        },
        {
            "pro_id": 52,
            "pro_nome": "MARACUJA 2A"
        },
        {
            "pro_id": 53,
            "pro_nome": "MANGA THOMY TIPO 15"
        },
        {
            "pro_id": 56,
            "pro_nome": "MAMAO PAPAIA TIPO 18"
        },
        {
            "pro_id": 59,
            "pro_nome": "MAMAO PAPAI 21"
        },
        {
            "pro_id": 60,
            "pro_nome": "COCO SECO 10"
        },
        {
            "pro_id": 61,
            "pro_nome": "COCO SECO 20"
        },
        {
            "pro_id": 64,
            "pro_nome": "MELAO TIPO 10"
        },
        {
            "pro_id": 66,
            "pro_nome": "MELAO TIPO7"
        },
        {
            "pro_id": 68,
            "pro_nome": "MACA FUJI 100 CAT1"
        },
        {
            "pro_id": 70,
            "pro_nome": "MACA FUJI 150"
        },
        {
            "pro_id": 71,
            "pro_nome": "MACA GALA CAT1 120"
        },
        {
            "pro_id": 73,
            "pro_nome": "MACA GALA 120 CAT2"
        },
        {
            "pro_id": 76,
            "pro_nome": "MELANCIA JAPONESA"
        },
        {
            "pro_id": 77,
            "pro_nome": "MELANCIA MEDIA"
        },
        {
            "pro_id": 78,
            "pro_nome": "MELANCIA PEQUENA"
        },
        {
            "pro_id": 79,
            "pro_nome": "MACA GALA150"
        },
        {
            "pro_id": 80,
            "pro_nome": "MACA GALA180"
        },
        {
            "pro_id": 81,
            "pro_nome": "MACA GALA216"
        },
        {
            "pro_id": 82,
            "pro_nome": "MACA GALA 135 CAT 1"
        },
        {
            "pro_id": 84,
            "pro_nome": "MACA FUJI120"
        },
        {
            "pro_id": 85,
            "pro_nome": "MACA FUJI GRANEL"
        },
        {
            "pro_id": 86,
            "pro_nome": "MACA GALA GRANEL"
        },
        {
            "pro_id": 87,
            "pro_nome": "MACA ARGENTINA100"
        },
        {
            "pro_id": 88,
            "pro_nome": "MACA VERDE "
        },
        {
            "pro_id": 89,
            "pro_nome": "PERA T90"
        },
        {
            "pro_id": 90,
            "pro_nome": "UVA RED GLOB"
        },
        {
            "pro_id": 91,
            "pro_nome": "UVA TOPSON"
        },
        {
            "pro_id": 92,
            "pro_nome": "PESSEGO IMPORT"
        },
        {
            "pro_id": 94,
            "pro_nome": "KIWI IMPORT"
        },
        {
            "pro_id": 95,
            "pro_nome": "NECTARINA "
        },
        {
            "pro_id": 96,
            "pro_nome": "PERA ESPANHOLA "
        },
        {
            "pro_id": 97,
            "pro_nome": "PERA PORTUGUESA"
        }
    ]
}

But I'm gettig this exception Unterminated object at character 1927 of "MY JSON STRING"

This is my method that does the HTTP request to the URL:

  public String ConsultaServidor(String URL_STRING) throws IOException {
        InputStream is = null;

        try {
            URL url = new URL(URL_STRING);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(15000);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.connect();
            conn.getResponseCode();

            is = conn.getInputStream();

            Reader reader = null;
            reader = new InputStreamReader(is);
            char[] buffer = new char[4096];
            reader.read(buffer);
            return new String(buffer);
        } finally {
            if (is != null) {
                is.close();
            }
        }
    }

Can Someone help me with this?

4
  • @AnkitNigam I'm accctualy parsing JSON to String.. Commented Jul 17, 2015 at 19:57
  • Can you also print what's in new String(buffer) to make sure that what you're receiving is exactly the same JSON ? Commented Jul 17, 2015 at 19:57
  • 1
    Reading exactly 4096 characters? This means the string is going to have lots of \0 characters that were not in the original stream, or that it's going to be cut in the middle. Why are you doing a single buffer read instead of a line-based read loop? Commented Jul 17, 2015 at 19:59
  • @RealSkeptic Can you provide an example of how can I do it? Commented Jul 17, 2015 at 20:01

3 Answers 3

1

Its better to use some buffer while reading like BufferedReader instead using InputStreamReader alone like :-

BufferedReader br = new BufferedReader(new InputStreamReader(is));
String s = null;
StringBuilder sb = new StringBuilder ();
while((s = br.readLine()) != null )
sb.append(s); 
return sb.toString();
Sign up to request clarification or add additional context in comments.

1 Comment

@henrique if the solution worked for you , you can mark it as correct so that others can also make use of it
0

Using a single buffer read from an input stream means that if you have less characters than there are in the buffer, your string is going to end up with a lot of zero characters (ASCII NUL - '\u0000') at the end.

If you have more characters than the size of the buffer, then your string is going to contain only the first 4096 characters, so it's cut in the middle.

The usual way to read an input stream is using a loop. You can either read the entire input stream into memory, or use a JSON parser that consumes the input stream directly.

Since I don't know what JSON parser you are using, I'll demonstrate one method.

        is = conn.getInputStream();

        StringBuilder sb = new StringBuilder();

        try ( Reader reader = new InputStreamReader(is) ) {
            char[] buffer = new char[4096];
            int numRead;
            while ( (numRead = reader.read(buffer)) != -1 ) {
                sb.append(buffer, 0, numRead );
            }
        } catch ( IOException e ) {
            e.printStackTrace(); // Or other exception handling
        }

        return sb.toString();

Note that in this method, you read the stream all the way to the end, which is when the value returned from the read is -1. And every time you only append the number of characters that were actually read (which is not necessarily 4096!) to the string builder.

Also note that you have a try-with-resources making sure you close the reader when you're done.

Another way would be to use a BufferedReader and read line by line until the end of file.

Comments

0

The solution was changing the code. And like Ankit said, using a BufferedReader..
The final code is this:

 public String ConsultaServidor(String URL_STRING) throws IOException {
        //InputStream is = null;

        try {
            URL url = new URL(URL_STRING);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(15000);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.connect();
            conn.getResponseCode();


            // is = conn.getInputStream();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    conn.getInputStream()));

            // Reader reader = null;
            // reader = new InputStreamReader(is);
            String inputLine;
            String responseData;
            responseData = "";
            while ((inputLine = in.readLine()) != null) // read till you can receive any data
                responseData += inputLine;
            in.close();
            return new String(responseData);
        } finally {
            //if (in != null) {
            //    in.close();
            // }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.