0

i'm trying to make a andoid application using youtube. i'd like to upload a video using URL. with my codes, i received 200 http status code and success message, but actually it doesn't. how can i resolve it?

URL url = new URL(urlString);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("PUT");
            conn.setRequestProperty("Authorization", String.format("Bearer %s", access_token));
            conn.setRequestProperty("Content-Type", "video/*");
            conn.setRequestProperty("Content-Length", ContentLength);

            File file = new File(path);

            FileInputStream fis = new FileInputStream(file);
            DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

            int numberBytes = fis.available();
            byte bytearray[] = new byte[numberBytes];
            Log.e(" FileLength", String.valueOf(bytearray.length));
            for(int i = 0; i < bytearray.length; i++)
                dos.write(bytearray[i]);
            dos.flush();

            fis.close();
            dos.close();

            int responseCode = conn.getResponseCode();
            if(responseCode == 200) {
                Log.e("ResponseCode", String.valueOf(responseCode));

                InputStream is = conn.getInputStream();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] byteBuffer = new byte[1024];
                byte[] byteData = null;
                int nLength = 0;
                while((nLength = is.read(byteBuffer, 0, byteBuffer.length)) != -1) {
                    baos.write(byteBuffer, 0, nLength);
                }
                byteData = baos.toByteArray();

                String response = new String(byteData);

                Log.e("RESPONSE", response);
            }

        } catch(Exception e) {
            e.printStackTrace();
        }
user1314742
  • 2,865
  • 3
  • 28
  • 34

1 Answers1

0

Use this library project

The code is a reference implementation for an Android OS application that captures video, uploads it to YouTube,

Detailed Answer: uploading using above library project

Community
  • 1
  • 1
karan
  • 3,319
  • 1
  • 35
  • 44