2

我正在尝试解析 ByteBuffer 对象中包含的 XML 数据,该对象是使用类似于以下 ReadBodyN() 的代码获得的。如果将 Httprequest 提供给一个小的 XML 页面,则 ReadBodyN() 和解析工作正常。但是对于在执行 ReadBodyN() 后包含超过 50k 个字符的大型 XML 页面,ByteBuffer 对象中只有 15559 个字符可用。如何将整个 XML 放入字节缓冲区?

 HttpResponse* pResponse = httpTransaction.GetResponse();
 if (pResponse->GetHttpStatusCode() == HTTP_STATUS_OK)
 {
     ByteBuffer* pBody = null;
     pBody = pResponse->ReadBodyN();
 }
4

1 回答 1

2

使用以下代码作为参考,在 tizen 中,我们将响应作为数据块获取,正如您所说的 15559 字节是参考数据。所以你应该收集字节缓冲区数据,直到你得到整个数据。

Code description:将字节缓冲区保留为类变量 ( here _pBuff)

_hasData是一个标志,一旦缓冲区有数据就设置(然后你需要附加数据)

一旦你得到整个数据清除_pbuff

void YourClass::OnTransactionReadyToRead(HttpSession& httpSession,
      HttpTransaction& httpTransaction, int availableBodyLen) {

 AppLog("Transaction Ready to Read : availableBodyLen %d", availableBodyLen);

 try {
      HttpResponse* pHttpResponse = null;
      HttpHeader* pHttpHeader = null;

      pHttpResponse = httpTransaction.GetResponse();

      if (pHttpResponse->GetHttpStatusCode() == HTTP_STATUS_OK) {

           bool _hasData = false;

           if (!_pBuff) {
                _pBuff = new ByteBuffer;
                _pBuff->Construct(availableBodyLen);
           }
           else
           {
                _pBuff->ExpandCapacity(_pBuff->GetCapacity() + availableBodyLen);
                _hasData = true;
           }

           pHttpHeader = pHttpResponse->GetHeader();

           if(_hasData)
           {
                ByteBuffer* pBody = pHttpResponse->ReadBodyN();

                // add to local store
                byte* pByte = new byte[availableBodyLen];
                pBody->GetArray(pByte,0,availableBodyLen);
                _pBuff->SetPosition(_pBuff->GetCapacity() - availableBodyLen);
                _pBuff->SetArray(pByte,0,availableBodyLen);
                delete []pByte;
                delete pBody;
           }
           else
                _pBuff = pHttpResponse->ReadBodyN();

    // Your Call || code
 }
 }
于 2013-11-14T18:52:44.843 回答