Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6044675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:02:19+00:00 2026-05-23T07:02:19+00:00

I’m having a software who is doing a http GET request and then I

  • 0

I’m having a software who is doing a http GET request and then I am receiving an HttpEntity response.
This is fine to me. The problem is that I want to use read this response 2 times and I dont know which way is the best.

If I convert the entity to a string, then when I try to access again the entity, Im getting an exception that the entity has been consumed.

If I try to use the getContent method to use the InputStream I dont find a way to reread the inputStream 2 times as i need.

Can someone tell me how I can save the httpEntity result as a way I can reuse it twice ?? Shoud I create a file ? How I do that ? What about performance to write a file each time I do a GET ? How to delete that file on each call ? Where to save the file ?

If you have any other ideas, thanks for the help.
I will appreciate code examples.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:02:19+00:00Added an answer on May 23, 2026 at 7:02 am

    I finally found how to convert a stream to a string so I did the following thing :

        //Get the answer from http request
        if(httpResponse!=null)
            entity = httpResponse.getEntity();
        else
            entity = null;
    
        //Display the answer in the UI
        String result;
        if (entity != null) {
                        //First, Open a file for writing
            FileOutputStream theXMLFile=null;
            try{
                theXMLFile=openFileOutput("HttpResponse.dat", MODE_PRIVATE);
            } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.e("ResultService Exception :", e.getMessage());
            }
    
            try {
                if(theXMLFile!=null) {
                                        //Save the stream to a file to be able to re read it later.
                    entity.writeTo(theXMLFile);
                                        //Entity is consumed now and cannot be reuse ! Lets free it.
                    entity=null;
    
                                        //Now, lets read this file !
                    FileInputStream theXMLStream=null;
                    try {
                        //Open the file for reading and convert to a string
                        theXMLStream = openFileInput("HttpResponse.dat");
                        result=com.yourutilsfunctionspackage.ServiceHelper.convertStreamToString(theXMLStream);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        Log.e("ResultService Exception :", e.getMessage());
                        result=null;
                    }
                    theXMLStream.close();
                    theXMLStream=null;
    
                    //Use the string for display
                    if(result!=null)
                        infoTxt.setText(getText(R.string.AnswerTitle) + " = " +result);
    
    try {
    //Reopen the file because you cannot use a FileInputStream twice.
    theXMLStream = openFileInput("HttpResponse.dat");
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    Log.e("ResultService Exception :", e.getMessage());
    }
    
    
    
    //Re use the stream as you want for decoding xml                
    if(theXMLStream!=null){
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder=null;
        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(builder!=null)
        {
            Document dom=null;
            try {
                dom = builder.parse(theXMLStream);
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
    
            if(dom!=null){
                Element racine = dom.getDocumentElement();
                NodeList nodeLst=racine.getElementsByTagName("response");
                Node fstNode = nodeLst.item(0);
                if(fstNode!=null){
                    Element fstElmnt = (Element) fstNode;
                    String CallingService=fstElmnt.getAttribute("service");
    
    etc....
    
    //Function taken from internet http://www.kodejava.org/examples/266.html
    public static String convertStreamToString(InputStream is) throws IOException {
            /*
             * To convert the InputStream to String we use the
             * Reader.read(char[] buffer) method. We iterate until the
             * Reader return -1 which means there's no more data to
             * read. We use the StringWriter class to produce the string.
             */
            if (is != null) {
                Writer writer = new StringWriter();
    
    
            char[] buffer = new char[1024];
            try {
                Reader reader = new BufferedReader(
                        new InputStreamReader(is, "UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                is.close();
            }
            return writer.toString();
        } else {       
            return null;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.