0

PHP スクリプトを使用して、データベース内のデータにアクセスしています。

ただし、プランが 3G アクセスをカバーしていない場合、データ転送は高価になる可能性があります。そのため、周囲に Wi-Fi がない場合は、アプリケーションが実際にどれだけダウンロード/アップロード/使用しているかを知っておくとよいでしょう。

アップロードしているバイト数 (ポスト引数) とダウンロードしているバイト数 (文字列出力/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 に答える