HttpURLConnection con = (HttpURLConnection) url.openConnection ();
InputStream in = con.getInputStream (); where a null pointer exception
Code for the open source code is as follows
// Download the ordinary plain text file
public String download (String urlStr) {
String line = null;
StringBuffer sb = new StringBuffer ();
BufferedReader bufr = null;
try {
// Create a URL object
url = new URL (urlStr);
Http // create a connection
HttpURLConnection con = (HttpURLConnection) url.openConnection ();
// Read the file
bufr = new BufferedReader (new InputStreamReader (con.getInputStream ()));
while ((line = bufr.readLine ())! = null) {
sb.append (line);
}
} Catch (Exception e) {
System.out.println ("text file download failed!");
} Finally {
if (bufr! = null)
try {
bufr.close ();
} Catch (IOException e) {
System.out.println ("Text File Close Failed to read the stream!");
}
}
return sb.toString ();
}
// Can download a variety of file
// Returns -1: Representative downloaded file error, returns 0: Download the file on behalf of a successful return to 1: representing the file already exists
// FileName mean you will have to deposit SD card file name, you can define your own file name
public int downloadFile (String url, String path, String fileName) {
InputStream in = null;
try {
FileUtils utils = new FileUtils ();
if (utils.isFileExist (path + fileName)) {
return 1;
} Else {
in = getInputStreamFromUrl (url);
File resultFile = utils.write2SDFromInputStream (path, fileName, in);
if (resultFile == null) {
return -1;
}
}
} Catch (IOException e) {
e.printStackTrace ();
return -1;
} Finally {
if (in! = null) {
try {
in.close ();
} Catch (IOException e) {
System.out.println ("read byte stream close failed!");
}
}
}
return 0;
}
// Get the package together according Url InputStream's capability to reuse
public InputStream getInputStreamFromUrl (String urlStr) throws IOException {
url = new URL (urlStr);
HttpURLConnection con = (HttpURLConnection) url.openConnection ();
InputStream in = con.getInputStream ();
return in;
}
}
Reply:
& Lt; uses-permission android: name = "android.permission.INTERNET" / & gt;
& Lt; uses-permission android: name = "android.permission.WRITE_EXTERNAL_STORAGE" / & gt;
& Lt; uses-permission android: name = "android.permission.MOUNT_UNMOUNT_FILESYSTEMS" / & gt;
Reply:
HttpURLConnection con = (HttpURLConnection) url.openConnection ();
InputStream in = con.getInputStream (); where null pointer exception
How with no
con.connect ()
Reply:
Upstairs said is, conn.connect ();
Reply:
There is no need conn.connect (); but still thank upstairs problem has been resolved
Reply:
The landlord, how to solve the problem, I also InputStream in = con.getInputStream (); This statement exception, with post upload http, and then send the data stream DataOutputStream of writeBytes, when you want to read the server returns the information, with which statement, the program died. . . . Solving the following code: String end = "\ r \ n";
String twoHyphens = "-";
String boundary = "--------------------------- 265001916915724";
try
{
String name = "username =";
// URL url = new URL ("http://202.96.99.115:6010/eodn/judge.jsp?judge=");
// String result = HttpUtil.queryStringForPost ("http://202.96.99.115:6010/eodn/Upload");
URL url = new URL ("http://202.96.99.115:6010/eodn/Upload");
HttpURLConnection con = (HttpURLConnection) url.openConnection ();
// Allow Input, Output, do not use the Cache
con.setDoInput (true);
con.setDoOutput (true);
con.setUseCaches (false);
// Set the transfer method = POST
con.setInstanceFollowRedirects (false);
con.setRequestMethod ("POST");
// Con.
// SetRequestProperty
con.setRequestProperty ("Connection", "Keep-Alive");
con.setRequestProperty ("Charset", "UTF-8");
con.setRequestProperty ("Content-Type",
"Multipart / form-data; boundary =" + boundary);
//con.setInstanceFollowRedirects(false);
con.connect ();
// Set DataOutputStream
// DataOutputStream ds =
// New DataOutputStream (con.getOutputStream ());
DataOutputStream ds =
new DataOutputStream (con.getOutputStream ());
//ds.writeBytes(twoHyphens + boundary + end);
//ds.writeBytes("Content-Disposition: form-data; "+
// "Name = \" file1 \ "; filename = \" "+
// NewName + "\" "+ end);
ds.writeBytes (twoHyphens + boundary);
ds.writeBytes ("Content-Disposition: form-data; name = \" "+ name +" \ "");
ds.writeBytes ("\ r \ n \ r \ n");
ds.writeBytes (nameMid);
ds.writeBytes (end);
// String contentUrl = twoHyphens + boundary + end + "Content-Disposition: form-data;" +
// "Name = \" file1 \ "; filename = \" "+
// NewName + "\" "+ end +" Content-Type: "+" application / octet-stream "
// + "\ R \ n \ r \ n";
ds.writeBytes (twoHyphens);
ds.writeBytes (boundary);
ds.writeBytes (end);
ds.writeBytes ("Content-Disposition: form-data; name = \" file \ "; filename = \" "+ newName +" \ "");
// Ds.writeBytes ("name = \" file \ "; filename = \" ");
// Ds.writeBytes (newName);
//ds.writeBytes ("\" ");
ds.writeBytes (end);
ds.writeBytes ("Content-Type:");
ds.writeBytes ("application / octet-stream");
ds.writeBytes ("\ r \ n \ r \ n");
// Get SQLDatabase objects
// SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase (
// DatabaseFilename, null);
// Get FileInputStream
file// FileInputStream fStream = new FileInputStream (DATABASE_PATH + "/" + "eodn.db");
FileInputStream f = new FileInputStream (uploadFile);
BufferedInputStream fStream = new BufferedInputStream (f);
// Set each write 1024bytes
int bufferSize = 1024;
byte [] buffer = new byte [bufferSize];
int length = -1;
// Read from the file data to the buffer
while ((length = fStream.read (buffer))! = -1)
{
// DataOutputStream to write the data in
ds.write (buffer, 0, length);
}
ds.writeBytes (end);
ds.writeBytes (twoHyphens + boundary + twoHyphens + end);
// Close streams
fStream.close ();
ds.flush ();
ds.close ();
// Get content
Response// InputStream is = con.getInputStream ();
// BufferedReader br = new BufferedReader (new InputStreamReader (is));
InputStream is = con.getInputStream ();
BufferedReader br = new BufferedReader (new InputStreamReader (is));
// InputStreamReader is = new InputStreamReader (con.getInputStream ());
// BufferedReader br = new BufferedReader (is);
String response = "";
String readLine = null;
while ((readLine = br.readLine ())! = null) {
// Response = br.readLine ();
response = response + readLine;
}
is.close ();
br.close ();
con.disconnect ();
// Int ch;
// StringBuffer b = new StringBuffer ();
// While ((ch = is.read ())! = -1)
// {
// B.append ((char) ch);
//}
// Will be displayed in the Dialog
ResponseshowDialog ("uploaded successfully" + response);
Reply:
Hello landlord I met with you the same question I ask you is empty inputStream is how to solve what causes Thank you.
Reply:
Talk about how to solve the problems be solved?
Reply:
Reply:
Inquired how to resolve landlord ~ ~ I encountered the same problem ah ah ah ~~~~~~
Reply:
? ? ? ? ? ? The landlord how to solve
No comments:
Post a Comment