希望各位回答的大神留下QQ等联系方式,万一遇见问题的时候方便请教。
最近在做一个android的软件,需要从服务器下载文件。我参照mars的文件下载的视频,先做了个小实验。不知道为什么urlConn.getInputStream()一直抛NullPointerException。以下是用到的部分代码(文章的最后有全部的代码):
url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
buffer = new BufferedReader( new InputStreamReader( urlConn.getInputStream() ) ); //经验证是urlConn.getInputStream()抛的Exception
1、我在AndroidManifest.xml文件中已经加了权限
<!--使用网络功能所需权限 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">
</uses-permission>
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE">
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE">
</uses-permission>
2、我在我手机的浏览器可以访问tomcat下urlStr指定的文件,所以urlStr应该没问题
以下是所有的代码:
Download.java
package com.example.download;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.example.utils.HttpDownloader;
public class Download extends Activity {
Button downloadTextButton;
Button downloadMP3Button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download);
downloadTextButton = (Button) findViewById(R.id.button1);
downloadTextButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub5
HttpDownloader httpDownloader = new HttpDownloader();
String text = httpDownloader.download("http://192.l68.0.106:8080/test/a1.txt");
System.out.println(text);
}
});
downloadMP3Button = (Button) findViewById(R.id.button2);
downloadMP3Button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
HttpDownloader httpDownloader = new HttpDownloader();
int result = httpDownloader.downFile("http://192.168.0.106:8080/test/a1.mp3", "test/", "a1.mp3");
System.out.println(result);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.download, menu);
return true;
}
}
HttpDownloader.java
package com.example.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpDownloader {
private URL url = null;
/**
* 根据URL下载文件,前提是这个文件当中的内容是文本,函数的返回值就是文本当中的内容
* 1.创建一个URL对象
* 2.通过URL对象,创建一个HttpURLConnection对象
* 3.得到InputStream
* 4.从InputStream当中读取数据
* @param urlStr
* @return
*/
public String download(String urlStr){
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
try {
url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
buffer = new BufferedReader( new InputStreamReader( urlConn.getInputStream() ) );
while( (line = buffer.readLine()) != null){
sb.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
buffer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
/**
*
* @param urlStr
* @param path
* @param fileName
* @return
* -1:文件下载出错
* 0:文件下载成功
* 1:文件已经存在
*/
public int downFile(String urlStr, String path, String fileName){
InputStream inputStream = null;
try {
FileUtils fileUtils = new FileUtils();