2

我得到一个非常长的字符串作为我在使用中收集它的 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();
}

任何帮助表示赞赏。

4

4 回答 4

3

可能不会完全打印到控制台,String但它确实存在。将其保存到文件以便查看。

于 2012-10-26T11:34:20.750 回答
3

您还可以将字符串拆分为字符串数组以查看所有字符串

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);
}
于 2012-10-26T11:40:27.560 回答
1

我不认为你的输入对于字符串来说太大了,但只是没有显示在控制台上,因为它不接受太长的行。无论如何,这是一个非常大的字符输入的解决方案:

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...");
    }
}
于 2012-10-26T12:30:32.613 回答
0

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;

                  }
于 2012-10-26T12:53:25.330 回答