Wednesday, April 23, 2014

Urgent! ! ! android file download urlConn.getInputStream () throwing NUllPointerException


I hope that answers the great God to leave QQ and other contact information in case of problems encountered when convenient to ask.
Recently made an android software, you need to download files from the server. I am referring to mars video file to download, do first a little experiment. Do not know why urlConn.getInputStream () has been throwing NullPointerException. Here are some of the code used (last have all the code of the article):
 
url = new URL (urlStr);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection ();
buffer = new BufferedReader (new InputStreamReader (urlConn.getInputStream ())); / / Proven is urlConn.getInputStream () throw Exception

1, I have added AndroidManifest.xml file permissions
 
<-! Required permission to use network functions ->









2, I'm on my phone's browser to access tomcat under urlStr specified file, so it should be no problem
urlStr
Here are all the codes:
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;

/ **
* According to the URL to download the file, provided that the contents of this document which is a text, the function's return value is the contents of the text which
* 1. Create a URL object
* 2 via a URL object, create an object
HttpURLConnection* 3. Obtain InputStream
* 4. Reading data from InputStream which
* @ 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: File download error
* 0: File successfully downloaded
* 1: The file already exists
* /
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 (IOException e) {
e.printStackTrace ();
}
}
return 0;
}

/ **
* According to the URL to get the input stream
* @ Param urlStr
* @ Return
* /
public InputStream getInputStreamFromURL (String urlStr) {
HttpURLConnection urlConn = null;
InputStream inputStream = null;
try {
url = new URL (urlStr);
urlConn = (HttpURLConnection) url.openConnection ();
inputStream = urlConn.getInputStream ();

} Catch (MalformedURLException e) {
e.printStackTrace ();
} Catch (IOException e) {
e.printStackTrace ();
}

return inputStream;
}
}


FileUtils.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;

/ **
* According to the URL to download the file, provided that the contents of this document which is a text, the function's return value is the contents of the text which
* 1. Create a URL object
* 2 via a URL object, create an object
HttpURLConnection* 3. Obtain InputStream
* 4. Reading data from InputStream which
* @ 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: File download error
* 0: File successfully downloaded
* 1: The file already exists
* /
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 (IOException e) {
e.printStackTrace ();
}
}
return 0;
}

/ **
* According to the URL to get the input stream
* @ Param urlStr
* @ Return
* /
public InputStream getInputStreamFromURL (String urlStr) {
HttpURLConnection urlConn = null;
InputStream inputStream = null;
try {
url = new URL (urlStr);
urlConn = (HttpURLConnection) url.openConnection ();
inputStream = urlConn.getInputStream ();

} Catch (MalformedURLException e) {
e.printStackTrace ();
} Catch (IOException e) {
e.printStackTrace ();
}

return inputStream;
}
}


activity_download.xml
 
xmlns: tools = "http://schemas.android.com/tools"
android: layout_width = "match_parent"
android: layout_height = "match_parent"
android: paddingBottom = "@ dimen / activity_vertical_margin"
android: paddingLeft = "@ dimen / activity_horizontal_margin"
android: paddingRight = "@ dimen / activity_horizontal_margin"
android: paddingTop = "@ dimen / activity_vertical_margin"
tools: context = "Download.">



AndroidManifest.xml
 

package = "com.example.download"
android: versionCode = "1"
android: versionName = "1.0">

android: minSdkVersion = "8"
android: targetSdkVersion = "14" />

android: allowBackup = "true"
android: icon = "@ drawable / ic_launcher"
android: label = "@ string / app_name"
android: theme = "@ style / AppTheme">
android: name = "com.example.download.Download"
android: label = "@ string / app_name">








<-! Required permission to use network functions ->












<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
http request to put the child thread.
Reply:
I see the video is not the child thread, you can achieve download
Reply:
4.0 to be used in sub-thread access network
Reply:
Thank you, finally AsyncTask achieved. Reference http://www.cnblogs.com/xiaoluo501395377/p/3430542.html

No comments:

Post a Comment