如何在 KSOAP2 中将 transport.dump 大小从 256 字节增加到 512 或更多字节?

Posted

技术标签:

【中文标题】如何在 KSOAP2 中将 transport.dump 大小从 256 字节增加到 512 或更多字节?【英文标题】:How to increase transport.dump size from 256 bytes to 512 or more bytes in KSOAP2? 【发布时间】:2013-08-03 06:37:18 【问题描述】:

我有来自 KSOAP2 库的 HttpTransportSE 对象。 我想转储可能包含微尘的响应文件,然后是简单的 9697 字符。 目前我正在通过运输来做到这一点。

transport.debug = true;
System.out.println("Response ----------"+transport.responseDump);

但它最终给了我一半的回应 ...

在其内部编码结构中,我发现它使用 256 字节来创建和销毁它的 responseDump,如下所示:

package org.ksoap2.transport;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.util.List;
import org.ksoap2.HeaderProperty;
import org.ksoap2.SoapEnvelope;
import org.xmlpull.v1.XmlPullParserException;

public class HttpTransportSE extends Transport

  private ServiceConnection connection;

  public HttpTransportSE(String url)
  
    super(null, url);
  

  public HttpTransportSE(Proxy proxy, String url)
  
    super(proxy, url);
  

  public HttpTransportSE(String url, int timeout)
  
    super(url, timeout);
  

  public HttpTransportSE(Proxy proxy, String url, int timeout) 
    super(proxy, url, timeout);
  

  public void call(String soapAction, SoapEnvelope envelope)
    throws IOException, XmlPullParserException
  
    call(soapAction, envelope, null);
  

  public List call(String soapAction, SoapEnvelope envelope, List headers)
    throws IOException, XmlPullParserException
  
    if (soapAction == null) 
      soapAction = "\"\"";
    
    byte[] requestData = createRequestData(envelope);

    this.requestDump = (this.debug ? new String(requestData) : null);
    this.responseDump = null;

    this.connection = getServiceConnection();

    this.connection.setRequestProperty("User-Agent", "kSOAP/2.0");

    if (envelope.version != 120) 
      this.connection.setRequestProperty("SOAPAction", soapAction);
    
    this.connection.setRequestProperty("Content-Type", "text/xml");
    this.connection.setRequestProperty("Connection", "close");
    this.connection.setRequestProperty("Content-Length", "" + requestData.length);

    if (headers != null) 
      for (int i = 0; i < headers.size(); i++) 
        HeaderProperty hp = (HeaderProperty)headers.get(i);
        this.connection.setRequestProperty(hp.getKey(), hp.getValue());    
this.connection.setRequestMethod("POST");
    this.connection.connect();

    OutputStream os = this.connection.openOutputStream();

    os.write(requestData, 0, requestData.length);
    os.flush();
    os.close();
    requestData = null;

    List retHeaders = null;
    InputStream is;
    try  this.connection.connect();
      is = this.connection.openInputStream();
      retHeaders = this.connection.getResponseProperties();
     catch (IOException e) 
      is = this.connection.getErrorStream();

      if (is == null) 
        this.connection.disconnect();
        throw e;
      
    

    if (this.debug) 
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      byte[] buf = new byte[256];
      while (true)
      
        int rd = is.read(buf, 0, 256);
        if (rd == -1)
          break;
        bos.write(buf, 0, rd);
      

      bos.flush();
      buf = bos.toByteArray();
      this.responseDump = new String(buf);
      is.close();
      is = new ByteArrayInputStream(buf);
    

    parseResponse(envelope, is);
    return retHeaders;
  

  public ServiceConnection getConnection() 
    return (ServiceConnectionSE)this.connection;
  

  protected ServiceConnection getServiceConnection() throws IOException 
    return new ServiceConnectionSE(this.proxy, this.url, this.timeout);
  

  public String getHost()
  
    String retVal = null;
    try
    
      retVal = new URL(this.url).getHost();
     catch (MalformedURLException e) 
      e.printStackTrace();
    

    return retVal;
  

  public int getPort()
  
    int retVal = -1;
    try
    
      retVal = new URL(this.url).getPort();
     catch (MalformedURLException e) 
      e.printStackTrace();
    

    return retVal;
  

  public String getPath()
  
    String retVal = null;
    try
    
      retVal = new URL(this.url).getPath();
     catch (MalformedURLException e) 
      e.printStackTrace();
    

    return retVal;
  

你发现它只有一个

int rd = is.read(buf, 0, 256);

那么,有没有办法增加 responseDump 的大小?

【问题讨论】:

答案是:if (transport.debug) byte[] is = transport.responseDump.getBytes();字符串路径="/mnt/sdcard/appData/";文件 file = new File(path+"responseDump.xml"); if (!file.exists()) file.createNewFile(); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); bos.write(is); bos.flush(); bos.close(); 感谢 esentsov。 【参考方案1】:

在 ksoap 中没有限制,但在 logcat 中。 Logcat不打印长字符串,所以写在一个文件里,或者在日志里一块一块的写。

【讨论】:

如果您看到这个 ksoap2 的内部代码,它只占用 256 个字节。因此,即使我尝试逐字节写入,它也不是完整的 responseDump。仍然 ii 你认为它可能对解决方案做出友好的回应。再次感谢! 它在一个循环中逐段读取输入流,缓冲区大小为 256 字节,而不仅仅是前 256 字节。 check this【参考方案2】:
if (transport.debug) 
 
byte[] is = transport.responseDump.getBytes(); 
String path="/mnt/sdcard/appData/"; 
File file = new File(path+"responseDump.xml"); 
if (!file.exists()) 
 
file.createNewFile();

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); bos.write(is); bos.flush(); bos.close(); 

【讨论】:

以上是关于如何在 KSOAP2 中将 transport.dump 大小从 256 字节增加到 512 或更多字节?的主要内容,如果未能解决你的问题,请参考以下文章

如何将本地 xml 文件转换为 org.ksoap2.serialization.SoapObject?

在Android 中使用KSOAP2调用WebService(转)

使用具有复杂对象的 KSoap2 调用 WCF 服务。 WCF 接收空值

带有proguard的Ksoap2

Ksoap2 Android - 如何为复杂对象的子属性指定命名空间?

通过ksoap2-android来调用Web Service操作的实例