Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android HttpEntity对象。。。重复使用它的最佳方法?_Android_Http_Entity - Fatal编程技术网

Android HttpEntity对象。。。重复使用它的最佳方法?

Android HttpEntity对象。。。重复使用它的最佳方法?,android,http,entity,Android,Http,Entity,我有一个软件正在执行http GET请求,然后我收到一个HttpEntity响应。 这对我很好。问题是,我想用阅读这个回答两次,我不知道哪种方式是最好的 如果我将实体转换为字符串,那么当我再次尝试访问该实体时,我会收到一个异常,即该实体已被使用 如果我尝试使用getContent方法来使用InputStream,我找不到一种方法来根据需要重新读取InputStream 2次 有人能告诉我如何保存httpEntity结果,这样我可以重复使用它两次吗??我应该创建一个文件吗?我怎么做到的?每次执行G

我有一个软件正在执行http GET请求,然后我收到一个HttpEntity响应。 这对我很好。问题是,我想用阅读这个回答两次,我不知道哪种方式是最好的

如果我将实体转换为字符串,那么当我再次尝试访问该实体时,我会收到一个异常,即该实体已被使用

如果我尝试使用getContent方法来使用InputStream,我找不到一种方法来根据需要重新读取InputStream 2次

有人能告诉我如何保存httpEntity结果,这样我可以重复使用它两次吗??我应该创建一个文件吗?我怎么做到的?每次执行GET时写入文件的性能如何?如何在每次通话中删除该文件?在哪里保存文件

如果你还有其他想法,谢谢你的帮助。
我将欣赏代码示例。

我终于找到了如何将流转换为字符串,因此我做了以下事情:

    //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;
    }
}

将结果保存为字符串并在以后需要结果时重新读取该字符串有什么问题?好吧,我需要知道如何将该字符串转换为XML dom对象,因为我需要对其进行解码,但我不知道如何进行。我知道从流中读取xml。。。但实体的流一旦使用一次就会被消耗。