Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在使用HttpURLConnection获取远程url内容时,如何保持换行和空行?_Java_Android_Newline_Httpurlconnection_Blank Line - Fatal编程技术网

Java 在使用HttpURLConnection获取远程url内容时,如何保持换行和空行?

Java 在使用HttpURLConnection获取远程url内容时,如何保持换行和空行?,java,android,newline,httpurlconnection,blank-line,Java,Android,Newline,Httpurlconnection,Blank Line,我使用以下代码使用android HttpURLConnection方法从我的网站获取一些数据,并将其放在webview textarea上。但不幸的是,获取的数据忽略了所有换行和空行。专家能告诉我如何在我的最终结果中保留空行和新行吗?提前谢谢 test.Data.php数据: helloWorld1 next2 next3 输出: helloWorld1next2next3 预期产出: helloWorld1 next2 next3 Android代码: public Strin

我使用以下代码使用android HttpURLConnection方法从我的网站获取一些数据,并将其放在webview textarea上。但不幸的是,获取的数据忽略了所有换行和空行。专家能告诉我如何在我的最终结果中保留空行和新行吗?提前谢谢

test.Data.php数据:

helloWorld1

next2
next3
输出:

helloWorld1next2next3  
预期产出:

helloWorld1

next2
next3
Android代码:

public String sendGetRequest() {

        try {

            StrictMode.setThreadPolicy(new Builder().permitAll().build());

            HttpURLConnection myURLConnection = (HttpURLConnection) new URL("http://mywebsite.com/testData.php").openConnection();
            myURLConnection.setReadTimeout(60000);
            myURLConnection.setConnectTimeout(60000);
            myURLConnection.setRequestMethod("GET");
            myURLConnection.setUseCaches(false);
            myURLConnection.setDoInput(true);
            myURLConnection.setDoOutput(true);
            myURLConnection.setRequestProperty("Content-Type", "text");
            myURLConnection.setRequestProperty("User-Agent", "Dalvik/2.1.0 (Linux; U; Android 6.0;");

            OutputStream os = myURLConnection.getOutputStream();

            os.close();

            myURLConnection.connect();

            BufferedReader in = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));


            StringBuffer sb = new StringBuffer();


            String line;

            while ((line = in.readLine()) != null) {

                sb.append(line);

            }
            in.close();


            // Update your UI
            mWebView.loadUrl("javascript:Parseit('" + sb.toString() + "');");


        } catch (Exception e) {

        }
        return null;

    }
webview上的javascript:

function Parseit(data)
{

var myTextArea = document.getElementById('area1');
myTextArea.innerHTML += data+"\n";

};

在追加字符串时添加特征线:

 while ((line = in.readLine()) != null) {

                sb.append(line);
                sb.append("\n");
            }

谢谢你的回复。我已经试过了,但没用。我在其他帖子中读到,当使用readline()时,换行符和空行符会丢失!但我不知道如何保存它们!您使用这种方法的结果是什么?因为正如您看到的代码,它在您阅读的每一行中添加了一个新的换行符。可能您的服务器响应只有一行?如果在浏览器上键入testData.php的url,示例数据将按预期显示在多行中(与上面的预期输出相同),因此服务器响应不能是一行。即使我查看testData.php的源代码,数据也是多行的!好的,使用这种方法的结果是什么?你能把里面的每一行都打印出来吗?Log.d(“标签”,行);某人附加(行);结果使用sb.append(行);某人附加(“\n”);是这样的:helloWorld1
next2
next3。我必须提到,我使用了不同的方法,手动将
添加到我的服务器数据(testData.php)中,以查看这是否会产生差异,但仍然在一行中!(如果我不把这些
放在不同的行上,只在不同的行上键入数据,那么当我在浏览器上键入url时,所有数据都显示在一行中!)。因此,在webview textarea中,带或不带
的sever data会在一行上生成所有数据!