解决方案 »

  1.   

    <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"/>
      

  2.   

    HttpURLConnection con =  (HttpURLConnection) url.openConnection();
     InputStream in = con.getInputStream(); 这里空指针异常
    中间怎么没有
    con.connect()
      

  3.   

    楼上所言极是,conn.connect(); 
      

  4.   

    这里不需要conn.connect();   但还是感谢楼上的  问题已经解决
      

  5.   

    楼主,问题如何解决的,我也是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);
      

  6.   

    你好 楼主 我遇到和你同样的问题 inputStream为空  请问是什么原因引起的  你是怎么解决的谢谢。