2013-08-02 25 views
10

Tôi có đối tượng HttpTransportSE từ thư viện KSOAP2. Tôi muốn kết xuất tệp phản hồi có thể chứa ghi chú rồi đơn giản 9697 ký tự. hiện tại tôi đang thực hiện việc này bằng cách thực hiện giao thông.Làm thế nào để tăng kích thước transport.dump từ 256 byte lên 512 byte hoặc nhiều hơn trong KSOAP2?

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

Nhưng nó mang lại cho tôi một nửa phản ứng với ... cuối cùng.

Trong cấu trúc mã hóa nội bộ của mình tôi thấy rằng nó đang sử dụng 256 byte để tạo và phá hủy nó responseDump như hình dưới đây:

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

Bạn phát hiện ra rằng chỉ mình

int rd = được .read (buf, 0, 256);

Vì vậy, có bất kỳ tùy chọn nào để tăng phản hồiKích thước không?

+0

trả lời là: if (transport.debug) { byte [] là = transport.responseDump.getBytes(); Đường dẫn chuỗi = "/ mnt/sdcard/appData /"; Tệp tệp = tệp mới (đường dẫn + "responseDump.xml"); nếu (! File.exists()) { file.createNewFile(); } BufferedOutputStream bos = new BufferedOutputStream (tệp FileOutputStream mới (tệp)); bos.write (là); bos.flush(); bos.close(); } Cảm ơn esentsov. –

Trả lời

1

Không có giới hạn trong ksoap, nhưng nó ở dạng logcat. Logcat không in các chuỗi dài, vì vậy hãy viết vào một tập tin hoặc viết từng mảnh trong nhật ký.

+0

Nếu bạn thấy mã nội bộ này của ksoap2, nó chỉ lấy 256 byte. vì vậy ngay cả khi tôi đang cố gắng để viết byte bằng byte của nó không hoàn thành responseDump. Tuy nhiên ii bạn nghĩ rằng nó có thể vui lòng đáp ứng với giải pháp. Và một lần nữa cảm ơn !. –

+0

Nó đọc một dòng đầu vào trong một chu kỳ mảnh bằng mảnh với kích thước bộ đệm 256 byte, không chỉ 256 byte đầu tiên. – esentsov

+0

[kiểm tra điều này] (http://stackoverflow.com/questions/8888654/android-set-max-length-of-logcat-messages) – esentsov

0
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(); 
} 
Các vấn đề liên quan