如何减少应用数据的使用?

时间:2014-11-15 02:48:02

标签: android http-post

我有一个Android应用,每5分钟发送一次带有GPS数据的HTTP POST。 POST本身(包括标题和正文)的大小小于1KB。响应也小于1KB。但是,我注意到我的应用程序每次POST都消耗40KB。在试验并消除了可能有助于数据使用的所有其他因素之后,我确定POST就是这样的事情。 消耗40KB。 HTTP POST消耗这么多数据是正常的吗?或者它与Android操作系统处理网络调用的方式有关,这会增加如此多的开销?以下是我用来执行POST的代码。我正在使用Apache HTTP库。

public String callWebMethod(
        Context poContext, 
        String psRestUrl,
        String psMethodNameWithGetParameters, 
        JSONObject psPostParameters,
        long pnTimeout) {

    String sHttpPostUrl = "";
    StringEntity paramEntity = null;
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = null;
    HttpResponse httpResponse = null;
    String sConvertedResponse = "";
    HttpParams httpParams = new BasicHttpParams();

    try {
        // Set entity
        paramEntity = new StringEntity(psPostParameters.toString());
        paramEntity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

        // Send POST
        sHttpPostUrl = psRestUrl + "/" + psMethodNameWithGetParameters;
        HttpConnectionParams.setConnectionTimeout(httpParams, (int) pnTimeout);
        HttpConnectionParams.setSoTimeout(httpParams, (int) pnTimeout);
        httpPost = new HttpPost(sHttpPostUrl);
        httpPost.setParams(httpParams);
        httpPost.setHeader("Content-Type", "application/json");
        httpPost.setHeader("Accept", "application/xml");
        httpPost.setEntity(paramEntity);
        httpResponse = httpClient.execute(httpPost);

        // Get response
        if (httpResponse == null) {
            Log.w(TAG, "No http response");
            return null;
        }

        sConvertedResponse = EntityUtils.toString(httpResponse.getEntity());

        return sConvertedResponse;
    }
    catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

0 个答案:

没有答案