0

我正在使用 PHP 脚本来访问我的数据库中的数据。

但是,如果您的计划不涵盖 3G 访问,数据传输可能会很昂贵。因此,如果周围没有 Wifi,最好知道我的应用程序实际下载/上传/使用了多少。

有没有一种方法可以确定我要上传多少字节(后参数)然后下载(字符串输出/php 脚本的响应)?

使用代码:

//http post
try {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    httppost.setEntity(new UrlEncodedFormEntity(arguments));
    HttpResponse httpresponse = httpclient.execute(httppost); 
    HttpEntity entity = httpresponse.getEntity();
    is = entity.getContent();
    Log.e("log_tag", "connection success ");
} catch(Exception e) {
    Log.e("log_tag", "Error in http connection "+e.toString());
}
4

1 回答 1

1

用于getEntity().getContentLength()在 HTTP 请求和响应中获取 HTTP 消息大小。并保存该consumed属性SharedPreferences以在另一个会话中恢复它。

代码将如下所示:

static long consumed = 0;

    //http post
    long consumedNow = 0;
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        httppost.setEntity(new UrlEncodedFormEntity(arguments));
        consumedNow += httppost.getEntity().getContentLength();
        HttpResponse httpresponse = httpclient.execute(httppost);
        HttpEntity entity = httpresponse.getEntity();
        is = entity.getContent();
        consumedNow += httpresponse.getEntity().getContentLength(); 
        Log.e("log_tag", "connection success ");
    } catch(Exception e) {
        Log.e("log_tag", "Error in http connection "+e.toString());
    } finally {
        consumed += consumedNow;
        SharedPreferences sp = context.getSharedPreferences("preferences", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putLong("consumed", value);
        editor.commit();
    }
于 2013-11-08T17:13:54.607 回答