------------------------------------------------------------------------------
-----------这里是问题解决方案---------------------------------------------------
----------------------------------------------------------------------------------
你不知道的背景是:Android4.0之后,主线程内是不允许进行下载任务的(原因相信你也知道)
于是我们要做的就是把程序的onClickListener()做一下小的修改,把下载任务放到非主线程进行就行了
case R.id.button_show_data:new Thread() {public void run() {HttpDownhelper httpDownhelper = new HttpDownhelper();String myString = httpDownhelper.showdata("http://192.168.56.1:8080/mp3/nothing.txt");System.out.println(myString);};}.start();
------------------------------------------------------------------------------
发件人:
发送时间: 2013-08-27 23:36
收件人:
主题: Freedown
朋友,你好!
我是一个Android初学者,最近学习遇到一点问题,自己实在无法解决,所以想请教下,希望你能不吝赐教,谢谢。
------------------------------------------------------------------------------
-----------这里是问题描述---------------------------------------------------
当我通过HttpURLConnection 来下载网络文本文件,并显示出来的时候,无法显示,我自己初步认为是读取网络文件的时候出错了,但是自己无法找出错误所在。。。
PS1.网络文件来源,我是通过tomcat解决的,使用其他程序可以成功调用相关文档没有问题。
PS2.已添加 <uses-permission android:name="android.permission.INTERNET"/>
PS3.附件是整个程序的源码
----------------------------------------------------------------------------------
------------------------------------------------------------------------------
-----------这里是程序源码,主要贴上来两部分源码---------------------------------------------------
------------第一部分是Activity,用来设定按钮监听,显示showdata()函数返回的String---------------
package com.example.freedown;
import charles.utils.HttpDownhelper;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
public class Choose_list extends Activity implements OnClickListener{
Button showdataButton,downloadButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_choose_list);showdataButton = (Button)findViewById(R.id.button_show_data);showdataButton.setOnClickListener(this);downloadButton = (Button)findViewById(R.id.button_download_data);downloadButton.setOnClickListener(this);
}
@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.button_download_data:break;case R.id.button_show_data:HttpDownhelper httpDownhelper = new HttpDownhelper();String myString = httpDownhelper.showdata("http://192.168.56.1:8080/mp3/nothing.txt");System.out.println(myString);break;default:break;}}
}
----------------------------------------------------------------------------------
------------第二部分是实现通过HttpURLConnection 读取网络文件的程序---------------
package charles.utils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpDownhelper {
private URL url = null;public String showdata(String address) {StringBuffer sb = new StringBuffer();String lineString = null;BufferedReader bufferedReader = null;try {url =new URL(address);HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));while ((lineString=bufferedReader.readLine())!=null){sb.append(lineString);}} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}finally{try {bufferedReader.close();} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}return sb.toString();}
}
----------------------------------------------------------------------------------
这里是——你的朋友,猪小德!!
如果方便,麻烦你回复下表示你收到这封邮件!Thx!