非常长的字符串作为Web服务的响应

时间:2012-10-26 11:32:06

标签: java android string

我收到一个非常长的字符串作为我使用StringBuilder收集它的Web服务的响应但是我无法获得我也使用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 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分: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);
}

答案 2 :(得分: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...");
    }
}

答案 3 :(得分: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;

                  }