Java 从url获取文本并显示它(几乎有效)
我试图从URL中获取文本并将其显示为字符串。URL以.txt结尾,如www.gains.com/more.txt所示。这些文本很长,最大大小为1MB。我正在尝试使用Java 从url获取文本并显示它(几乎有效),java,android,android-asynctask,Java,Android,Android Asynctask,我试图从URL中获取文本并将其显示为字符串。URL以.txt结尾,如www.gains.com/more.txt所示。这些文本很长,最大大小为1MB。我正在尝试使用AsyncTask。问题是代码有时是有效的。它在我第一次运行代码时起作用,第二次它没有显示文本。有时应用程序会显示文本,有时则不会。这是怎么回事?这是我的密码 class RequestTask extends AsyncTask<String, String, String>{ @Override
AsyncTask
。问题是代码有时是有效的。它在我第一次运行代码时起作用,第二次它没有显示文本。有时应用程序会显示文本,有时则不会。这是怎么回事?这是我的密码
class RequestTask extends AsyncTask<String, String, String>{
@Override
// username, password, message, mobile
protected String doInBackground(String... url) {
// constants
int timeoutSocket = 5000;
int timeoutConnection = 5000;
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpClient client = new DefaultHttpClient(httpParameters);
HttpGet httpget = new HttpGet(url[0]);
try {
HttpResponse getResponse = client.execute(httpget);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if(statusCode != HttpStatus.SC_OK) {
Log.w("MyApp", "Download Error: " + statusCode + "| for URL: " + url);
return null;
}
String line = "";
StringBuilder total = new StringBuilder();
HttpEntity getResponseEntity = getResponse.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(getResponseEntity.getContent()));
while((line = reader.readLine()) != null) {
total.append(line);
}
line = total.toString();
story.add(line); //story is my array i use to display the text
return line;
} catch (Exception e) {
Log.w("MyApp", "Download Exception : " + e.toString());
}
return null;
}
@Override
protected void onPostExecute(String result) {
//This is empty i dont know what it does
}
}
我还有一个小问题,当文本被显示时,我会丢失文本的格式,就像我丢失了段落之间的空格而得到一个大段落一样。有办法解决这个问题吗?我应该使用另一种方法吗?Http请求并不总是占用相同的精确时间。你试过增加超时时间吗?5000 mil秒不是很多,尤其是当文件大小达到1MB时。调用显示功能,该功能将显示下载文件后保存的文件中的文本 使用onPostExecute
@Override
protected void onPostExecute(String result) {
//You add the code where you call the text from the file saved
}
您可以从调用线程的主活动调用函数
这样,只有在下载和保存文本的过程完成后,才会显示视图
编辑:这是一个示例
TextPage textPage; // the activity that calls the AsyncTask
List<String> story = new ArrayList<String>();
GetTextInfoTask(TextPage textPage) {
this.textPage = textPage;
}
... // your doInBackground function here
@Override
protected void onPostExecute(Object objR){
// A toast is displayed in the TextPage Activity once the data is finished downloading
Toast.makeText(textPage.getBaseContext(), story.get(0),
Toast.LENGTH_LONG).show();
}
TextPage TextPage;//调用异步任务的活动
列表故事=新的ArrayList();
获取任务(TextPage TextPage){
this.textPage=textPage;
}
... // 你的背景函数在这里
@凌驾
受保护的void onPostExecute(对象objR){
//数据下载完成后,将在TextPage活动中显示toast
Toast.makeText(textPage.getBaseContext(),story.get(0),
Toast.LENGTH_LONG).show();
}
我将其增加到100倍以上。我还是不明白,这就是我想做的。故事。添加(行);如何调用onPostExecute并执行此操作?保留故事。添加(行);在doInBackground函数中。要在onPostExecute函数中添加的是当UI处理视图中文本的显示时。onPostExecute在doInBackground函数完成执行后自动运行。
TextPage textPage; // the activity that calls the AsyncTask
List<String> story = new ArrayList<String>();
GetTextInfoTask(TextPage textPage) {
this.textPage = textPage;
}
... // your doInBackground function here
@Override
protected void onPostExecute(Object objR){
// A toast is displayed in the TextPage Activity once the data is finished downloading
Toast.makeText(textPage.getBaseContext(), story.get(0),
Toast.LENGTH_LONG).show();
}