0

I need to upload video to Youtube in Android app.

I tried to upload video via HTTP Post method ..

First i get Authorization token form GoogleLogin auth.

Then i tried to upload video with HTTP Post request.

Below are my code..

        HttpURLConnection conn = null;
        // BufferedReader br = null;
        DataOutputStream dos = null;
        InputStream inStream = null;

        // InputStream is = null;
        // OutputStream os = null;
        // boolean ret = false;
        // String StrMessage = "";
        String path = "/home/siva/test_data/";
        String existingFileName = "test5.3gp";
        File videoFile = new File(str);

        // String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "b93dcbA3";
        String ver = "2";

        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 2048;
        // String responseFromServer = "";
        try {
            FileInputStream fileInputStream = new FileInputStream(str);

            URL url = new URL(
                    "http://uploads.gdata.youtube.com/feeds/api/users/default/uploads");
            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("Host", "uploads.gdata.youtube.com");
            conn.setRequestProperty("Authorization", "GoogleLogin auth="
                    + token);
            conn.setRequestProperty("GData-Version", ver);
            conn.setRequestProperty("X-Gdata-Client", clientId);
            conn.setRequestProperty("X-GData-Key", "key=" + developerKey);
            conn.setRequestProperty("Slug", "VID_20120502_122127.3gp");
            conn.setRequestProperty("Content-Type",
                    "multipart/related;boundary=" + boundary);
            // conn.setRequestProperty("Content-Length", ""+videoFile.length());
            conn.setRequestProperty("Connection", "close");
            conn.setRequestProperty("Content-Type",
                    "application/atom+xml;charset=UTF-8");
            //conn.setRequestProperty("Content-Type", "video/*");
            conn.setRequestProperty("Content-Type", "video/3gpp");
            //conn.setRequestProperty("Content-Transfer-Encoding", "binary");
            conn.setRequestProperty("Content-Transfer-Encoding", "UTF-8");


            StringBuilder test_xml = new StringBuilder();
            test_xml.append("<?xml version='1.0'encoding='UTF-8'?>\n");
            test_xml.append("<entry xmlns=\"http://www.w3.org/2005/Atom\"\n");

            test_xml.append("xmlns:media=\"http://search.yahoo.com/mrss/\"\n");
            test_xml.append("xmlns:yt=\"http://gdata.youtube.com/schemas/2007\">\n");
            test_xml.append("<media:group>\n");
            test_xml.append("<media:title type=\"plain\">TestVideo</media:title>\n");
            test_xml.append("<media:descriptiontype=\"plain\">\nTest Video\n</media:description>\n");
            test_xml.append("<media:category\n");
            test_xml.append("scheme=\"http://gdata.youtube.com/schemas/2007/categories.cat\">People\n");
            test_xml.append("</media:category>\n");
            test_xml.append("<media:keywords>toast,wedding</media:keywords>\n");
            test_xml.append("</media:group>\n");
            test_xml.append("</entry>");

            String requestBody = new String(test_xml.toString().getBytes(),"UTF-8");
        int lngth = requestBody.length();
        conn.setRequestProperty("Content-Length", ""+lngth);

        dos = new DataOutputStream(conn.getOutputStream());
            dos.write((twoHyphens + boundary).toString().getBytes());
            dos.write(test_xml.toString().getBytes("UTF-8"));

            // System.out.println(test_xml.toString());
            dos.write((twoHyphens + boundary).toString().getBytes());

            // 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.flush();
                dos.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }


            dos.write((twoHyphens + boundary + twoHyphens).toString()
                    .getBytes());

            // close streams
            fileInputStream.close();
            dos.flush();
            dos.close();

        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        StringBuilder outputBuilder = new StringBuilder();
        try {
            int responseCode = conn.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                inStream = conn.getInputStream();
            } else {
                inStream = conn.getErrorStream();
            }

            String string;
            if (inStream != null) {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(inStream));
                while (null != (string = reader.readLine())) {

                    outputBuilder.append(string).append("\n");
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (inStream != null) {
                try {
                    inStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

I am getting error as below.

05-05 15:43:18.391: W/System.err(2938): java.io.IOException: exceeded content-length limit of 476 bytes
05-05 15:43:18.391: W/System.err(2938):     at org.apache.harmony.luni.internal.net.www.protocol.http.RetryableOutputStream.write(RetryableOutputStream.java:58)
05-05 15:43:18.391: W/System.err(2938):     at java.io.DataOutputStream.write(DataOutputStream.java:99)
05-05 15:43:18.391: W/System.err(2938):     at java.io.FilterOutputStream.write(FilterOutputStream.java:105)
05-05 15:43:18.391: W/System.err(2938):     at com.VidoePick.VideoPickActivity.testvideo(VideoPickActivity.java:158)
05-05 15:43:18.391: W/System.err(2938):     at com.VidoePick.VideoPickActivity.onActivityResult(VideoPickActivity.java:73)
05-05 15:43:18.391: W/System.err(2938):     at android.app.Activity.dispatchActivityResult(Activity.java:3908)
05-05 15:43:18.391: W/System.err(2938):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
05-05 15:43:18.391: W/System.err(2938):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
05-05 15:43:18.391: W/System.err(2938):     at android.app.ActivityThread.access$2000(ActivityThread.java:117)
05-05 15:43:18.391: W/System.err(2938):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
05-05 15:43:18.391: W/System.err(2938):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 15:43:18.391: W/System.err(2938):     at android.os.Looper.loop(Looper.java:130)
05-05 15:43:18.391: W/System.err(2938):     at android.app.ActivityThread.main(ActivityThread.java:3683)
05-05 15:43:18.391: W/System.err(2938):     at java.lang.reflect.Method.invokeNative(Native Method)
05-05 15:43:18.391: W/System.err(2938):     at java.lang.reflect.Method.invoke(Method.java:507)
05-05 15:43:18.391: W/System.err(2938):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-05 15:43:18.391: W/System.err(2938):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-05 15:43:18.391: W/System.err(2938):     at dalvik.system.NativeStart.main(Native Method)
05-05 15:43:18.401: W/System.err(2938): java.io.IOException: content-length promised 476 bytes, but received 10
05-05 15:43:18.401: W/System.err(2938):     at org.apache.harmony.luni.internal.net.www.protocol.http.RetryableOutputStream.close(RetryableOutputStream.java:48)
05-05 15:43:18.401: W/System.err(2938):     at org.apache.harmony.luni.internal.net.www.protocol.http.RetryableOutputStream.contentLength(RetryableOutputStream.java:64)
05-05 15:43:18.401: W/System.err(2938):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.prepareRequestHeaders(HttpURLConnectionImpl.java:864)
05-05 15:43:18.401: W/System.err(2938):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.writeRequestHeaders(HttpURLConnectionImpl.java:787)
05-05 15:43:18.401: W/System.err(2938):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1028)
05-05 15:43:18.401: W/System.err(2938):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:726)
05-05 15:43:18.401: W/System.err(2938):     at com.VidoePick.VideoPickActivity.testvideo(VideoPickActivity.java:195)
05-05 15:43:18.401: W/System.err(2938):     at com.VidoePick.VideoPickActivity.onActivityResult(VideoPickActivity.java:73)
05-05 15:43:18.401: W/System.err(2938):     at android.app.Activity.dispatchActivityResult(Activity.java:3908)
05-05 15:43:18.401: W/System.err(2938):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
05-05 15:43:18.401: W/System.err(2938):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
05-05 15:43:18.401: W/System.err(2938):     at android.app.ActivityThread.access$2000(ActivityThread.java:117)
05-05 15:43:18.401: W/System.err(2938):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
05-05 15:43:18.401: W/System.err(2938):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 15:43:18.401: W/System.err(2938):     at android.os.Looper.loop(Looper.java:130)
05-05 15:43:18.401: W/System.err(2938):     at android.app.ActivityThread.main(ActivityThread.java:3683)
05-05 15:43:18.401: W/System.err(2938):     at java.lang.reflect.Method.invokeNative(Native Method)
05-05 15:43:18.401: W/System.err(2938):     at java.lang.reflect.Method.invoke(Method.java:507)
05-05 15:43:18.401: W/System.err(2938):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-05 15:43:18.401: W/System.err(2938):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-05 15:43:18.401: W/System.err(2938):     at dalvik.system.NativeStart.main(Native Method)

Any help would be greatly appreciated.

4

2 回答 2

1

如果您只想上传,那么您可以使用

安卓-ytd

通过分步完成教程查看此答案

于 2012-05-05T10:36:17.383 回答
0

我有同样的错误。您需要检查“Content-Length”属性

conn.setRequestProperty("Content-Length", "" + lngth);
于 2013-10-16T13:08:58.207 回答