0

オブジェクトがサーバーに送信されたときに進行状況バーを表示したい。しかし、 doInBackground()メソッドにどのコードを書く必要があるのか​​、 onProgressUpdate ( ) にどのコードを書く必要があるのか​​ わかりません 。

ここでは、Client Socket プログラムを使用してオブジェクトをサーバーに送信します。

コード (内部クラス):

class DownloadFileAsync extends AsyncTask<String, String, String> {


@Override
protected void onPreExecute() {
dialog = new ProgressDialog(this);
    dialog.setMessage("Uploading...");
    dialog.setIndeterminate(false);
    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    dialog.setProgress(0);
    dialog.show();
}

@Override
protected String doInBackground(String... aurl) {
}

@Override
protected void onProgressUpdate(String... progress) {

}

@Override
protected void onPostExecute(String unused) {

}
}

だから、ここでdoInBackground()onProgressUpdate () に書く必要があるもの

サーバーに送信する必要があるオブジェクトserverObjectが 1 つあります。

現在、次のコードを使用して、進行状況バーなしでオブジェクトをサーバーに送信しています。

Socket s = new Socket("xxx.xx.xx.xxx", xxx);

//to send object to the server
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(serverObject);
oos.flush();
if (s != null) {s.close();}
if (oos != null) {oos.close();}

このコードをdoInBackground()メソッドに書き込んで、送信されるデータの割合を追跡する方法。

オブジェクトのサイズを取得する方法。多くのサイトで、ファイルのサイズについて言及しています。しかし、私の場合、これはオブジェクトです。

そして、これを実行するためにもう1つ書く必要があります:

new DownloadFileAsync().execute(params);

では、このパラメータを取得する方法。

4

1 に答える 1

1

こんにちは、これがあなたの質問の答えです。リンクを見つけてください

これは、ファイルをサーバーにアップロードする方法の例です。

プログレスバーを追加できる例を次に示します。

ProgressDialog pd;
private final Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        switch(msg.what){
         case 1:
            pd = ProgressDialog.show(this, "uploading...", "Please Wait..", true,false);
            pd.setCancelable(false);

          break;
         case 2:
         pd.dismiss();
         break;
        }

}
};
 new Thread(new Runnable() {                
            @Override
            public void run() {                     
                try {                   
                         handler.sendEmptyMessage(1);
                         executeMultipartPost(bitmap,filename.jpg);
                         handler.sendEmptyMessage(2);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }   

            }
        }).start();
于 2012-06-11T12:08:45.197 に答える