2012-06-22 5 views
7

現在の特定のプロジェクトに問題があり、今すぐ停止しているような気がします。私はHTTPポストとマルチパートのフォームデータを使ってビデオをアップロードしようとしています。私は、HTTPプロトコルと具体的にはマルチパートのフォームデータを理解する上で壁にぶつかるような気がします。AndroidはHTTPマルチパートフォームデータを使用してリモートサーバーにビデオをアップロードします

動画をアップロードするURLがhttp://videoupload.thecompany.com/VideoApp.xml?method=upload&objectType=person&objectId=777777の形式です。また、タイトル、説明、およびvideoFileも含める必要があります。これらは「マルチパートデータ」ですか?

は、私は私のニーズUpload video from Android to server?を満たすために、このソリューションを適応しようとしたし、次の余分なデータを設定したすべてのそうのような他のconn.setRequestPropertyは()を呼び出します。しかし、これは私のために働いていない

conn.setRequestProperty("title", "video title"); 
conn.setRequestProperty("description", "video description"); 

。コードの元の作者から、30行後のマルチパートフォームデータを追加するコメントがありますが、なぜそれが分からないのですか?助けてくれてありがとう。

答えて

19

ここでは、私が思いついた2つのステップの解決方法を紹介します。情報とリンクは主にhereです。このソリューションは、関連するSOの投稿のうちのいくつかでupload2server()メソッドよりも把握が容易でした。これが他の人に役立つことを願っています

1)ギャラリーからビデオファイルを選択します。

変数を作成します。private static final int SELECT_VIDEO = 3; - 後でチェックするものであれば、使用する数字は関係ありません。次に、インテントを使用してビデオを選択します。

Intent intent = new Intent(); 
intent.setType("video/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(Intent.createChooser(intent,"Select a Video "), SELECT_VIDEO); 

uploadVideo()メソッドを起動するには、onActivityResult()を使用します。あなたはそれがあなたの作業をしたら、おそらくます

private void uploadVideo(String videoPath) throws ParseException, IOException { 

    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost(YOUR_URL); 

    FileBody filebodyVideo = new FileBody(new File(videoPath)); 
    StringBody title = new StringBody("Filename: " + videoPath); 
    StringBody description = new StringBody("This is a description of the video"); 

    MultipartEntity reqEntity = new MultipartEntity(); 
    reqEntity.addPart("videoFile", filebodyVideo); 
    reqEntity.addPart("title", title); 
    reqEntity.addPart("description", description); 
    httppost.setEntity(reqEntity); 

    // DEBUG 
    System.out.println("executing request " + httppost.getRequestLine()); 
    HttpResponse response = httpclient.execute(httppost); 
    HttpEntity resEntity = response.getEntity(); 

    // DEBUG 
    System.out.println(response.getStatusLine()); 
    if (resEntity != null) { 
     System.out.println(EntityUtils.toString(resEntity)); 
    } // end if 

    if (resEntity != null) { 
     resEntity.consumeContent(); 
    } // end if 

    httpclient.getConnectionManager().shutdown(); 
} // end of uploadVideo() 

public void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if (resultCode == RESULT_OK) { 

     if (requestCode == SELECT_VIDEO) { 
      System.out.println("SELECT_VIDEO"); 
      Uri selectedVideoUri = data.getData(); 
      selectedPath = getPath(selectedVideoUri); 
      System.out.println("SELECT_VIDEO Path : " + selectedPath); 

      uploadVideo(selectedPath); 
     }  
    } 
} 

private String getPath(Uri uri) { 
    String[] projection = { MediaStore.Video.Media.DATA, MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DURATION}; 
    Cursor cursor = managedQuery(uri, projection, null, null, null); 
    cursor.moveToFirst(); 
    String filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA)); 
    int fileSize = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE)); 
    long duration = TimeUnit.MILLISECONDS.toSeconds(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION))); 


    //some extra potentially useful data to help with filtering if necessary 
    System.out.println("size: " + fileSize); 
    System.out.println("path: " + filePath); 
    System.out.println("duration: " + duration); 

    return filePath; 
} 

2)あなたのプロジェクトに追加し、最新のHttpClient jarファイルをダウンロードし、http://hc.apache.org/downloads.cgiに移動し、以下の方法を使用してビデオをアップロードそれをスレッドに入れてアップロードダイアログを追加したいと思っていますが、これはあなたを始めます。 upload2Server()メソッドをうまく試してみると、私のために働いています。これは、マイナーな調整を加えて画像やオーディオにも機能します。

+0

いいですよ!これは大容量ビデオに使用できますか?または、別のファイルタイプに使用できますか? –

+1

@AliBagheriShakibは、より大きな動画では問題なく動作します。はい、オーディオクリップやその他のファイルタイプに非常によく似ています。何か問題がある場合は教えてください。 –

+1

はい@Kyle Cleggです。それは私のために完璧に働いた。私の問題は仲間を解決:)私自身の経験を投稿する:http://stackoverflow.com/q/23504191/2624611 –

関連する問題