2012-07-18 86 views
2

我需要发送的一系列HTTP请求。 AFAIK它会影响电池的使用。处理这类问题有没有特别的策略?频繁的HTTP请求

UPD:我必须使用POST请求,这是我的POST请求的功能:

URL url; 
    HttpURLConnection connection = null; 
    try { 
    //Create connection 
    url = new URL(targetURL); 
    connection = (HttpURLConnection)url.openConnection(); 
    connection.setRequestMethod("POST"); 
    connection.setConnectTimeout(3000); 
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 

    connection.setRequestProperty("Content-Length", "" + 
      Integer.toString(urlParameters.getBytes().length)); 
    connection.setRequestProperty("Content-Language", "en-US"); 

    if(headers != null){ 
     for (String key : headers.keySet()) { 
      connection.setRequestProperty(key, headers.get(key)); 
     } 
    } 

    connection.setUseCaches (false); 
    connection.setDoInput(true); 
    connection.setDoOutput(true); 

    //Send request 
    DataOutputStream wr = new DataOutputStream (
       connection.getOutputStream()); 
    wr.writeBytes (urlParameters); 
    wr.flush(); 
    wr.close(); 

    //Get Response 
    InputStream is = connection.getInputStream(); 
    BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
    String line; 
    StringBuffer response = new StringBuffer(); 
    while((line = rd.readLine()) != null) { 
     response.append(line); 
     response.append('\r'); 
    } 
    rd.close(); 

    Log.d("response", response.toString()); 

    return response.toString(); 

    } catch (Exception e) { 

    e.printStackTrace(); 
    return null; 

    } finally { 

    if(connection != null) { 
     connection.disconnect(); 
    } 
    } 

有没有什么可以做,以改善这个问题方面的性能;

+0

你能否详细说明后面,你正在试图解决这个问题? – nhahtdh 2012-07-18 13:00:46

+0

更新,看一看 - 有什么,应该进行修改,以对抗condiditions – user1462299 2012-07-18 13:10:20

回答

3

下面是从谷歌I视频/ O正是这个话题:

Google I/O

简而言之:

每个请求从待机状态唤醒无线电和原来充满电。请求发送后,它会保持这种状态一段时间,然后在低功耗状态下多呆一会儿,然后再次进入待机状态。

如果你做了一系列小的请求,每个请求将触发收音机到全功率,如果请求之间的时间间隔太短它永远不会进入待机模式。

但是,如果你积累了您的要求(如果您的应用程序并不真正需要这么经常显示一些远程数据),并将它们作为一批,但不是经常会触发无线电少,延长电池寿命。

1

不知道什么是你的问题,但你会莫名其妙地回答自己 - 更多的活动水渠电池多,小于活动做。这非常明显,因此,假设您想要降低电池使用量,特殊策略最终会减少您的活动。将您的HTTP呼叫分组,重做您的服务器端以提高效率,批量执行您的呼叫以使用无线电的全功率状态。