Android的读取URL的内容(内容后,中缺少结果) [英] Android Read contents of a URL (content missing after in result)
本文介绍了Android的读取URL的内容(内容后,中缺少结果)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下的code读取URL的内容
I have the following code that reads the content of a url
public static String DownloadText(String url){
StringBuffer result = new StringBuffer();
try{
URL jsonUrl = new URL(url);
InputStreamReader isr = new InputStreamReader(jsonUrl.openStream());
BufferedReader in = new BufferedReader(isr);
String inputLine;
while ((inputLine = in.readLine()) != null){
result.append(inputLine);
}
}catch(Exception ex){
result = new StringBuffer("TIMEOUT");
Log.e(Util.AppName, ex.toString());
}
in.close();
isr.close();
return result.toString();
}
问题是我缺少的含量,结果4065个字符后返回。有人可以帮我解决这个问题。
The problem is I am missing content after 4065 characters in the result returned. Can someone help me solve this problem.
请注意: 网址我想读包含一个JSON响应,以便一切都在同一行,我认为这就是为什么我有一些内容的缺失。
Note: The url I am trying to read contains a json response so everything is in one line I think thats why I am having some content missing.
推荐答案
试试这个:
try {
feedUrl = new URL(url).openConnection();
} catch (MalformedURLException e) {
Log.v("ERROR","MALFORMED URL EXCEPTION");
} catch (IOException e) {
e.printStackTrace();
}
try {
in = feedUrl.getInputStream();
json = convertStreamToString(in);
}catch(Exception e){}
而convertStreamToString是:
while convertStreamToString is:
private static String convertStreamToString(InputStream is) throws UnsupportedEncodingException {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
这篇关于Android的读取URL的内容(内容后,中缺少结果)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文