Hi i am using google drive rest api v2.i am trying to upload a file in a 
folder in android.i write a method for this.

public String uploadRequest(String token,byte[] body){
        URL url = null;
        String response="";
        String body1 = Base64.encodeToString(body, Base64.DEFAULT);
        Log.i("common-token", token);
        Log.i("common-body",body1.length()+"");
        String b = "--foo_bar_baz";
        b +="\r\nContent-Type: application/json; charset=UTF-8";
        b +="\r\n\r\n{\"title\":\"My 
File\",\"parents\":[{\"id\":\"0B5p7ccHwxN4Rak5QOGo1dUdtQk0\"}],\"mimeType\":\"image/png\"}";
        b +="\r\n\r\n--foo_bar_baz";
        b +="\r\nContent-Type: image/png";
        b +="\r\nContent-Transfer-Encoding: base64";
        b +="\r\n\r\n";
        Log.i("common-bb",b+"");
        try {

            url = new URL("https://www.googleapis.com/upload/drive/v2/files";);

            byte[] outputInBytes = b.getBytes();
            int len= 0;
            len += outputInBytes.length;
            Log.i("common-bb1",body1);
            byte[] outputInBytes1 = body1.getBytes();
            len += outputInBytes1.length;
            String b1 =" \r\n--foo_bar_baz--\r\n";
            Log.i("common-bb2",b1);
            byte[] outputInBytes2 = b1.getBytes();
            len += outputInBytes2.length;
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("POST");
            c.setDoInput(true);
            c.setDoOutput(true);
            c.setRequestProperty("Authorization", "Bearer " + token);
            c.setRequestProperty("Content-Type", "multipart/related; 
boundary=\"foo_bar_baz\";");
            c.setRequestProperty("Content-Length", "" + len);
            Log.i("common", "coming here");
            OutputStream os = c.getOutputStream();
            os.write(outputInBytes);
            Log.i("common", "coming here 3");
            os.write(outputInBytes1);
            Log.i("common", "coming here 4");
            os.write(outputInBytes2);
           // os.flush();
            os.close();
            Log.i("common", "length is: " + len);
            Log.i("common", "coming here1");
            Log.i("common-response",""+c.getResponseCode());
            Log.i("common-response",""+c.getResponseMessage());
            //Log.i("common-response",""+c.getContent());
           InputStream is= c.getErrorStream();
           Log.i("common","coming here1");
            InputStreamReader isr =new InputStreamReader(is);
            Log.i("common","coming here2");
           BufferedReader br=new BufferedReader(isr);
           StringBuilder sb = new StringBuilder();
            Log.i("common","coming here1");
            String line;
            while ((line = br.readLine()) != null)
                sb.append(line+"\n");

            br.close();
            Log.i("common","coming here 2");
            Log.i("common-error",sb.toString());
        } catch (MalformedURLException e) {
            Log.w("common-err",e.getMessage());
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();
            Log.w("common-err1",e.getMessage());

        }
  return "";
    }

In bytes i am sending

  File file = new File(j);
                int size = (int) file.length();
                byte[] bytes = new byte[size];

                        BufferedInputStream buf = new BufferedInputStream(new 
FileInputStream(file));
                        buf.read(bytes, 0, bytes.length);
                        buf.close();
                        c.uploadRequest(token,bytes);

i am getting Malformed multipart body error with 400 response code.i can't 
use google-drive-android api(https://developers.google.com/drive/android/) 
.i have to use google drive rest api.can anyone help with this error

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/910322ba-3264-4435-98d0-389da66a64a8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to