HttpURLConnection.getInputStream很慢 [英] HttpURLConnection.getInputStream very slow

查看:4487
本文介绍了HttpURLConnection.getInputStream很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HttpURLConnection.getInputStream一种使用同一个服务器端的服务相比,iPhone应用程序非常多的时间。

HttpURLConnection.getInputStream takes very much time when compared to iPhone App which uses the same server side services.

以下code为用于服务

The following code is used for the service :

         date= new java.util.Date();             
         Log.d("time","Time Stamp before posting  "+new Timestamp(date.getTime()));

         URL ur= new URL(url);           
         HttpURLConnection conn = (HttpURLConnection) ur.openConnection();
         conn.setRequestProperty("Connection", "close");
         conn.setReadTimeout(10000);
         conn.setConnectTimeout(15000);
         conn.setRequestMethod("POST");
         conn.setDoInput(true);
         conn.setDoOutput(true);             
         OutputStream os = conn.getOutputStream();
         BufferedWriter writer = new BufferedWriter(
                 new OutputStreamWriter(os, "UTF-8"));
         writer.write(getQuery(nameValuePairs));
         writer.close();
         os.close();
         conn.connect();

         StringBuffer response=null;             
         try{           
             Log.d("time","Time Stamp bfr InputStream  "+new Timestamp(date.getTime()));    

             InputStream is = conn.getInputStream();

             date= new java.util.Date();             
             Log.d("time","Time Stamp aftr InputStream  "+new Timestamp(date.getTime()));            

             BufferedReader rd = new BufferedReader(new InputStreamReader(is));
             String line;
             response = new StringBuffer(); 
             while((line = rd.readLine()) != null) {
                 response.append(line);
                 response.append('\r');
             }
             rd.close();
             response.toString();
             result=response.toString();

         } catch (Exception e) {

        }

要检查该服务需要一定的时间,我把日志条目打印时间戳。

To check where the service takes time, I put the log entries to print TimeStamp.

该过程的平均时间是如下:

The average time for the process is as follows :

平均时间发布到服务器只需不到2密耳秒结果
  创建输入流的平均时间花费了近5秒。

Average time for posting to server takes less than 2 Mil seconds
Average time for creating input stream takes almost 5 seconds

写入响应的平均时间少于2密耳秒

Average time for writing response is less than 2 mil seconds.

为什么输入流需要较长的时间,这使得整个服务任何想法很慢?

Any idea on why the input stream takes much time which makes the entire service very slow?

推荐答案

您不是衡量你在想什么你测量。没有被写入到服务器,直到你调用的getInputStream()或GETRESPONSE code()。所以,你真的测量:

You're not measuring what you think you're measuring. Nothing gets written to the server until you call getInputStream() or getResponseCode(). So you're really measuring:


  • 连接时间

  • 传输时间

  • 在服务器处理时间

当你认为你只是测量的getInputStream()时间。

when you think you're just measuring getInputStream() time.

原因在于,HttpURLConnection类自动设置内容长度报头,由缓冲所有的输出。您可以通过使用分块传输模式避免这种情况。那么至少,你会看到那里的时候是真的。

The reason is that HttpURLConnection auto-sets the content-length header, by buffering all the output. You can avoid that by using chunked transfer mode. Then at least you will see where the time is really going.

这篇关于HttpURLConnection.getInputStream很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆