簡體   English   中英

很長的字符串,作為Web服務的響應

[英]very long string as a response of web service

我得到一個很長的字符串作為Web服務的響應,我正在使用StringBuilder收集它,但是我無法獲得完整的值,我也使用了StringBuffer但沒有成功。

這是我正在使用的代碼:

private static String read(InputStream in ) throws IOException {
    //StringBuilder sb = new StringBuilder(1000);
    StringBuffer sb = new StringBuffer();
    String s = "";
    BufferedReader r = new BufferedReader(new InputStreamReader( in ), 1000);
    for (String line = r.readLine(); line != null; line = r.readLine()) {
        sb.append(line);
        s += line;
    } in .close();

    System.out.println("Response from Input Stream Reader >>>" + sb.toString());
    System.out.println("Response from Input S >>>>>>>>>>>>" + s);
    return sb.toString();
}

任何幫助表示贊賞。

String可能不會完全打印到控制台,但實際上它已經存在。 將其保存到文件中以查看它。

您還可以將字符串拆分為字符串數組,以查看所有字符串

String delimiter = "put a delimiter here e.g.: \n";
String[] datas=sb.toString().split(delimiter);

for(String string datas){
System.out.println("Response from Input S >>>>>>>>>>>>" + string);
}

我不認為您的輸入對於String來說太大,但不會顯示在控制台上,因為它不接受太長的行。 無論如何,這是一個非常大的字符輸入解決方案:

private static String[] readHugeStream(InputStream in) throws IOException {
    LinkedList<String> dataList = new LinkedList<>();
    boolean finished = false;
    //
    BufferedReader r = new BufferedReader(new InputStreamReader(in), 0xFFFFFF);
    String line = r.readLine();
    while (!finished) {
        int lengthRead = 0;
        StringBuilder sb = new StringBuilder();
        while (!finished) {
            line = r.readLine();
            if (line == null) {
                finished = true;
            } else {
                lengthRead += line.length();
                if (lengthRead == Integer.MAX_VALUE) {
                    break;
                }
                sb.append(line);
            }
        }
        if (sb.length() != 0) {
            dataList.add(sb.toString());
        }
    }
    in.close();
    String[] data = dataList.toArray(new String[]{});
    ///
    return data;
}

public static void main(String[] args) {
    try {
        String[] data = readHugeStream(new FileInputStream("<big file>"));
    } catch (IOException ex) {
        Logger.getLogger(StackoverflowStringLong.class.getName()).log(Level.SEVERE, null, ex);
    } catch (OutOfMemoryError ex) {
        System.out.println("out of memory...");
    }
}

System.out.println()不會打印所有字符,它只能在控制台中顯示有限數量的字符。 您可以在SD卡中創建文件,然后將字符串作為文本文檔復制到其中,以檢查您的確切響應。

 try
                {
                      File root = new File(Environment.getExternalStorageDirectory(), "Responsefromserver");
                      if (!root.exists()) 
                        {
                          root.mkdirs();
                      }
                      File gpxfile = new File(root, "response.txt");
                      FileWriter writer = new FileWriter(gpxfile);
                      writer.append(totalResponse);
                      writer.flush();
                      writer.close();
                  }
                  catch(IOException e)
                  {
                             System.out.println("Error:::::::::::::"+e.getMessage());
                      throw e;

                  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM