Url获取InputStream 空指针异常 URL 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> HttpURLConnection con = (HttpURLConnection) url.openConnection(); InputStream in = con.getInputStream(); 这里空指针异常中间怎么没有con.connect() 楼上所言极是,conn.connect(); 这里不需要conn.connect(); 但还是感谢楼上的 问题已经解决 楼主,问题如何解决的,我也是InputStream in = con.getInputStream();这条语句异常,用post方式上传http,然后用DataOutputStream的writeBytes发数据流,当想读取服务器返回信息时,用该条语句,程序就死了。求解,代码如下: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(); // 允许Input、Output,不使用Cache con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false); //设置传送的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(); // 设置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"); //得到SQLDatabase对象 // SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase( // databaseFilename, null); // 取得文件的FileInputStream //FileInputStream fStream = new FileInputStream(DATABASE_PATH+"/"+"eodn.db"); FileInputStream f= new FileInputStream(uploadFile); BufferedInputStream fStream=new BufferedInputStream(f); //设置每次写入1024bytes int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; int length = -1; //从文件读取数据至缓冲区 while((length = fStream.read(buffer)) != -1) { // 将资料写入DataOutputStream中 ds.write(buffer, 0, length); } ds.writeBytes(end); ds.writeBytes(twoHyphens + boundary + twoHyphens + end); // close streams fStream.close(); ds.flush(); ds.close(); // 取得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 ); // } //将Response显示于Dialog showDialog("上传成功"+response); 你好 楼主 我遇到和你同样的问题 inputStream为空 请问是什么原因引起的 你是怎么解决的谢谢。 HELLO WORLD 在安卓上如何做的?有实例吗? 关于谷歌地图的请教 调用系统谷歌 Content Provider安全性问题 关于Android 2.3中Email显示问题 急!!!安卓程序修改配置文件后不能及时更新 Android 如何用sql语句来操作系统库 网上那个HelloWorld出错了 android 实现im一般流行用方案? 有关Android广播和线程、、、 如何令按钮实时获取现时时间 android的socket的输入流的复杂处理 求助:eclipse创建android工程时最后一步create activity处无选项,下图...
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
InputStream in = con.getInputStream(); 这里空指针异常
中间怎么没有
con.connect()
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();
// 允许Input、Output,不使用Cache
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
//设置传送的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();
// 设置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");
//得到SQLDatabase对象
// SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
// databaseFilename, null);
// 取得文件的FileInputStream
//FileInputStream fStream = new FileInputStream(DATABASE_PATH+"/"+"eodn.db");
FileInputStream f= new FileInputStream(uploadFile);
BufferedInputStream fStream=new BufferedInputStream(f);
//设置每次写入1024bytes
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize]; int length = -1;
//从文件读取数据至缓冲区
while((length = fStream.read(buffer)) != -1)
{
// 将资料写入DataOutputStream中
ds.write(buffer, 0, length);
}
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary + twoHyphens + end); // close streams
fStream.close();
ds.flush();
ds.close();
// 取得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 );
// }
//将Response显示于Dialog
showDialog("上传成功"+response);