Question

Why is the following code only returning me a "{" i.e start of a JSON string and not the entire JSON? When i type the URL in the browser it returns me the complete JSON. I tried to buffer the response but nothing seems to be working?Can anyone explain why?

HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Nasik%20&types=geocode&language=en&sensor=true&key=API-KEY");
HttpResponse response = httpclient.execute(httpget);
InputStream is = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
Toast.makeText(this, br.readLine(), Toast.LENGTH_LONG).show();      
Was it helpful?

Solution

You're just reading the first line of your response.

Try something like that : http://www.java2s.com/Code/Android/File/ReadInputStreamwithBufferedReader.htm

OTHER TIPS

Try this way.

try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        Log.d("Json Output",sb.toString());
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

Update :

You need to read each line, currently you are trying to read first line.

You are using function br.readline(). As the function name suggests it reads only a single line. To parse it completely use something like

StringBuilder sb = new StringBuilder();
 String line = null;
 while ((line = br.readLine()) != null) {
    sb.append(line + "\n");
}
Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top