【发布时间】:2018-05-05 14:27:09
【问题描述】:
我正在使用以下代码使用 android HttpURLConnection 方法从我的网站获取一些数据并将其放在 webview textarea 上。但不幸的是,获取的数据忽略了所有换行符和空行。专家能否告诉我如何在最终结果中保留空行和新行?提前致谢。
test.Data.php 数据:
helloWorld1
next2
next3
输出:
helloWorld1next2next3
预期输出:
helloWorld1
next2
next3
安卓代码:
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";
};
【问题讨论】:
标签: java android newline httpurlconnection blank-line