菜鸟跟mars的视频,自学android中,但是今天这段视频老是报告空指针异常,把mars的源代码直接用项目导入可以使用,但是把代码复制到本人新建的项目中就报空指针异常,新手分不多,还望指点!根据logcat输出内容,HttpDownloader里面的76行出错,接着导致Activity里面的42行代码也出错,代码如下:
package com.victor.e08_download; import com.victor.e08_utils.HttpDownloader; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button txtButton; private Button mp3Button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtButton = (Button)findViewById(R.id.bt_Text); mp3Button = (Button)findViewById(R.id.bt_Mp3); txtButton.setOnClickListener(new DownloadTxtListener()); mp3Button.setOnClickListener(new DownloadMp3Listener()); } class DownloadTxtListener implements OnClickListener{ @Override public void onClick(View v) { HttpDownloader httpDownloader = new HttpDownloader(); String lrc = httpDownloader.download("http://192.168.1.107:8080/voa1500/a1.lrc"); System.out.println(lrc); } } class DownloadMp3Listener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub HttpDownloader httpDownloader = new HttpDownloader(); int result = httpDownloader.downFile("http://192.168.2.102:8080/1.doc", "voa/", "voa01"); System.out.println(result); } } }
—
package com.victor.e08_utils; import java.io.BufferedReader; import java.io.File; 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.得到InputStram * 4.从InputStream当中读取数据 * @param urlStr * @return */ public String download(String urlStr) { StringBuffer sb = new StringBuffer(); String line = null; BufferedReader buffer = null; try { // 创建一个URL对象 url = new URL(urlStr); // 创建一个Http连接 HttpURLConnection urlConn = (HttpURLConnection) url .openConnection(); // 使用IO流读取数据 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 (Exception e) { e.printStackTrace(); } } return sb.toString(); } /** * 该函数返回整形 -1:代表下载文件出错 0:代表下载文件成功 1:代表文件已经存在 */ public int downFile(String urlStr, String path, String fileName) { InputStream inputStream = null; try { FileUtils fileUtils = new FileUtils(); if (fileUtils.isFileExist(path + fileName)) { return 1; } else { inputStream = getInputStreamFromUrl(urlStr); File resultFile = fileUtils.write2SDFromInput(path,fileName, inputStream); if (resultFile == null) { return -1; } } } catch (Exception e) { e.printStackTrace(); return -1; } finally { try { inputStream.close(); } catch (Exception e) { e.printStackTrace(); } } return 0; } /** * 根据URL得到输入流 * * @param urlStr * @return * @throws MalformedURLException * @throws IOException */ public InputStream getInputStreamFromUrl(String urlStr) throws MalformedURLException, IOException { url = new URL(urlStr); HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); InputStream inputStream = urlConn.getInputStream(); return inputStream; } }
—
package com.victor.e08_utils; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.os.Environment; public class FileUtils { private String SDPATH; public String getSDPATH() { return SDPATH; } public FileUtils() { //得到当前外部存储设备的目录 // /SDCARD SDPATH = Environment.getExternalStorageDirectory() + "/"; } /** * 在SD卡上创建文件 * * @throws IOException */ public File creatSDFile(String fileName) throws IOException { File file = new File(SDPATH + fileName); file.createNewFile(); return file; } /** * 在SD卡上创建目录 * * @param dirName */ public File creatSDDir(String dirName) { File dir = new File(SDPATH + dirName); dir.mkdir(); return dir; } /** * 判断SD卡上的文件夹能否存在 */ public boolean isFileExist(String fileName){ File file = new File(SDPATH + fileName); return file.exists(); } /** * 将一个InputStream里面的数据写入到SD卡中 */ public File write2SDFromInput(String path,String fileName,InputStream input){ File file = null; OutputStream output = null; try{ creatSDDir(path); file = creatSDFile(path + fileName); output = new FileOutputStream(file); byte buffer[] = new byte[4 * 1024]; while((input.read(buffer)) != -1){ output.write(buffer); } output.flush(); } catch(Exception e){ e.printStackTrace(); } finally{ try{ output.close(); } catch(Exception e){ e.printStackTrace(); } } return file; } }
解决方案:20分
/**
* 根据URL得到输入流
*
* @param urlStr
* @return
* @throws MalformedURLException
* @throws IOException
*/
public InputStream getInputStreamFromUrl(String urlStr)
throws MalformedURLException, IOException {
url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConn.getInputStream();
return inputStream;
}
76行是inputStream为空, 那就看上面的方法里面的inputStream是不是返回的就是空. 假如返回的是空, 那你就看看你下载文件的路径能否正确. 你先用浏览器看一下http://192.168.2.102:8080/1.doc你这个路径能不能看到文件. 怎么感觉好像是你文件路径的问题.
* 根据URL得到输入流
*
* @param urlStr
* @return
* @throws MalformedURLException
* @throws IOException
*/
public InputStream getInputStreamFromUrl(String urlStr)
throws MalformedURLException, IOException {
url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConn.getInputStream();
return inputStream;
}
76行是inputStream为空, 那就看上面的方法里面的inputStream是不是返回的就是空. 假如返回的是空, 那你就看看你下载文件的路径能否正确. 你先用浏览器看一下http://192.168.2.102:8080/1.doc你这个路径能不能看到文件. 怎么感觉好像是你文件路径的问题.