0

ユーザー入力情報、テキスト、画像 (base64 文字列に変換) をサーバーにアップロードしようとしています。これを実現するために httpurlconnection を使用していますが、パフォーマンスが非常に遅くなります。画像のサイズは 600 X 600 で、アップロードには約 119 ~ 125 秒かかります。ここに私のコードがあります -

public class MyClass implements Runnable {
Thread thread = new Thread(this);
     public MyClass(String url_string.......) {
     thread.start();
     }

     @Override
     public void run()
     {
         String response = executeRequest();
         parseResponse(response);
     }
}

            URL url_string = prepareURL(url_string, method);
            url = new URL(url_string);
            httpUrlConnection = (HttpURLConnection) url.openConnection();
            httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
            httpUrlConnection.setConnectTimeout(25000);
            httpUrlConnection.setUseCaches(false);
            httpUrlConnection.setRequestMethod(method);
     //method variable in the above line is just a string constant (GET or POST)

            if(method.equalsIgnoreCase(Constants.POST)) {
                httpUrlConnection.setDoInput(true);
                httpUrlConnection.setDoOutput(true);
            }

          //toUseJson variable is us just a boolean to determine whether 
          //we have to set the chunking or fixedstreaming mode, 
          //based on GET method or POST method

            if(toUseJson) {
                //httpUrlConnection.setFixedLengthStreamingMode(json_message.getBytes().length);
                //httpUrlConnection.setChunkedStreamingMode(1024*8);
                httpUrlConnection.setChunkedStreamingMode(0);
            }

            httpUrlConnection.connect();

            System.out.println("start - " + System.currentTimeMillis());
            if(toUseJson) {
                output_stream = httpUrlConnection.getOutputStream();
                BufferedOutputStream buffered_output_stream = new BufferedOutputStream(output_stream);
                System.out.println("the bytes to read - " + json_message.getBytes().length);
                InputStream is = new ByteArrayInputStream(json_message.getBytes("UTF-8"));
                byte[] buffer = new byte[1024*1024];
                int count = 0;

                System.out.println("middle - " + System.currentTimeMillis());
                while((count = is.read(buffer)) != -1) {
                    System.out.println("count - " + count);
                    buffered_output_stream.write(buffer, 0, count);
                }

                output_stream.flush();
            }

.........

アップロード時間を短縮するためにさまざまな組み合わせを試していますが、常に 110 ~ 120 秒ほどかかります。json メッセージのバイト数 - 907237 バイト

どんな助けでも大歓迎です。ありがとう!

4

1 に答える 1

0

バッファ サイズを 1024*1024 ではなく 1024 にしてみてください。

1024Bは1KBなので、1KBずつコピーしてください。

あなたは1024KB = 1MBである1024 * 1024を割り当てていましたが、Androidではそれほど優雅ではありません;)

于 2013-08-18T10:28:19.640 に答える