Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • Al Hobbs
  • salvin francis

Constructing a Get Request in Java with embedded XML and converting the response as InputStream

Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a requirement whereby I need to construct a GET request from within my Java program that get from a publicly available URI a JSON of this shape:

I then need to convert the resulting response into an InputStream for some additional processing. I have tried a few things, mostly from here:

The issue I am encountering is that by the time I get to the InputStream I always face a closedStream issue:

Where am I going wrong and most importantly, is this a good approach?

Thank you,


Posts: 26912
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First you read the response into a String. That involves getting the data from the server and moving it into your application, roughly speaking. Second you want to read the response again, via an InputStream. But you can't because you already read the response.

So that's where you are going wrong. Is it a good approach? I don't think it is. Why not change your code so that you have one place which does all of the processing, instead of some code which does "processing" and other code which does "additional processing"?

But if you want a quick and dirty fix, you could get your InputStream from the String which you already got from the server. Convert the String to byte[] and use a ByteArrayInputStream; and don't forget to use the correct character set when you convert the String to the byte array. That's the character set which the server used to produce the response. Hopefully you can get it from your Response object or your Entity object.
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic