10

現在進行中のプロジェクトの特定の部分で問題が発生しており、行き詰まっているように感じます。HTTP 投稿とマルチパート フォーム データを使用して動画をアップロードしようとしています。HTTP プロトコル、特にマルチパート フォーム データを理解するのに壁にぶつかったような気がします。

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

このソリューションを自分のニーズに合わせて調整してみました Android からサーバーに動画をアップロードしますか? 、および他のすべての conn.setRequestProperty() 呼び出しに続く追加データを次のように設定します。

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

しかし、これは私にはうまくいきません。30行ほど後にマルチパートフォームデータを追加するコードの元の作成者からのコメントがありますが、その理由はわかりません。助けてくれてありがとう。

4

1 に答える 1

21

主にここにある情報とリンクから、私が思いついた2段階の解決策を次に示します。このソリューションは、関連する 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);

onActivityResult() を使用して、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) http://hc.apache.org/downloads.cgiに移動し、最新の HttpClient jar をダウンロードしてプロジェクトに追加し、次の方法を使用してビデオをアップロードします。

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( )

動作するようになったら、おそらくそれをスレッドに入れてアップロード ダイアログを追加したいと思うでしょうが、これで作業を開始できます。upload2Server() メソッドの試行に失敗した後、私のために働いています。これは、若干の微調整を行うことで、画像と音声にも機能します。

于 2012-06-22T21:35:29.960 に答える