Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从android应用程序向Soundcloud上传音频_Java_Android_Audio_File Upload_Soundcloud - Fatal编程技术网

Java 从android应用程序向Soundcloud上传音频

Java 从android应用程序向Soundcloud上传音频,java,android,audio,file-upload,soundcloud,Java,Android,Audio,File Upload,Soundcloud,我正在录制音频并尝试在soundcloud服务器上上载音频。但它不起作用。有人能纠正我哪里做错了。我搜索了很多,但没有任何东西对我有效。我是java初学者。我已经浪费了1天时间来解决这个问题 private class AsyncTaskRunner extends AsyncTask<String,Void,String> { @Override protected String doInBackground(String... par

我正在录制音频并尝试在soundcloud服务器上上载音频。但它不起作用。有人能纠正我哪里做错了。我搜索了很多,但没有任何东西对我有效。我是java初学者。我已经浪费了1天时间来解决这个问题

     private class AsyncTaskRunner extends AsyncTask<String,Void,String> {


        @Override
        protected String doInBackground(String... params) {
            doFileUpload();
            return null;
        }


    }


        private void doFileUpload(){
        HttpURLConnection conn = null;
        //DataOutputStream dos = null;
        //DataInputStream inStream = null;
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary =  "*****";
        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1*1024*1024;
        String responseFromServer = "";
        String urlString = "http://api.soundcloud.com/tracks";
        try
        {
            //------------------ CLIENT REQUEST
            FileInputStream fileInputStream = new FileInputStream(new File(AudioSavePathInDevice) );
            // open a URL connection to the Servlet
            URL url = new URL   (urlString);
            // Open a HTTP connection to the URL
            conn = (HttpURLConnection) url.openConnection();
            // Allow Inputs
            conn.setDoInput(true);
            // Allow Outputs
            conn.setDoOutput(true);
            // Don't use a cached copy.
            conn.setUseCaches(false);
            // Use a post method.
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
            DataOutputStream dos = new DataOutputStream( conn.getOutputStream() );
            //Adding oauth token
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"oauth_token\""+lineEnd+lineEnd+access_token+lineEnd);
//            dos.writeBytes(lineEnd);
            //Adding Track title
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"track[title]\""+lineEnd+lineEnd+contributor_name+lineEnd);
//            dos.writeBytes(lineEnd);
            //Track taglist
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"track[tag_list]\""+lineEnd+lineEnd+"Tagore Project"+lineEnd);
//            dos.writeBytes(lineEnd);

            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"track[asset_data]\";filename=\"" + AudioSavePathInDevice + "\"" + lineEnd);
//            dos.writeBytes(lineEnd);

            //Add sharing
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"track[sharing]\""+lineEnd+lineEnd+sharing+lineEnd);
            dos.writeBytes(lineEnd);
            // create a buffer of maximum size
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];
            // read file and write it into form...
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            while (bytesRead > 0)
            {
                dos.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }
            // send multipart form data necesssary after file data...
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
            // close streams
            Log.e("Debug","File is written");
            fileInputStream.close();
            dos.flush();
            dos.close();
        }
        catch (MalformedURLException ex)
        {
            Log.e("Debug", "error: " + ex.getMessage(), ex);
            Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show();
        }
        catch (IOException ioe)
        {
            Log.e("Debug", "error: " + ioe.getMessage(), ioe);
            Toast.makeText(this, ioe.getMessage(), Toast.LENGTH_SHORT).show();
        }
        //------------------ read the SERVER RESPONSE
//        try {
//            DataInputStream inStream = new DataInputStream ( conn.getInputStream() );
//            String str;
//            Log.e("Debug","Before while");
//            while (( str = inStream.readLine()) != null)
//            {
//                Log.e("Debug","Server Response "+str);
//            }
//            inStream.close();
//
//        }
            try (InputStream is = conn.getInputStream()) {
                BufferedReader lines = new BufferedReader(new InputStreamReader(is, "UTF-8"));
//                if(is == null) {
//                    Log.e("Debug","Reponse null ");
//                }
//                if(lines == null) {
//                    Log.e("Debug","Reponse null ");
//                }
//                int count = 0;
                while (true) {
                    Log.e("Debug","Server Response ");
                    String line = lines.readLine();
                    if (line == null) {
                        Log.e("Debug","Server Break");
                        break;

                    }
                }
            }
        catch (IOException ioex){
            Log.e("Debug", "error: " + ioex.getMessage(), ioex);
        }
    }
 new AsyncTaskRunner().execute();