2013-12-15 19 views
0

我想使用async-http-client將數據發佈到服務器並保持設備上的SQLite與此數據同步。使用異步HTTP客戶端 Android async-http-client:爲回調添加參數?

  • 更新SQLite的行中的onSuccess/onFailure處回調

    1. 惰性新數據的SQLite
    2. 後新的數據到服務器:所以基本上我想整個事情附加數據

    我的問題是:我如何在AsyncHttpResponseHandler中獲得正確的行ID?

    讓我們創建一個簡單的例子(沒有數據庫連接,以保持簡單但同樣的問題)。

    private void addPerson(name){ 
    
        //using a global client created like this in activity onCreate(): 
        //AsyncHttpClient client = new AsyncHttpClient(); 
    
        //here is a database insert creating a new row, returning the inserted id 
        int rowId = <some id returned by the insert>; 
    
        RequestParams param = new RequestParams(); 
        param.put("name", name); 
    
        client.post("http://www.my.service.url", param, new AsyncHttpResponseHandler() { 
        @Override 
        public void onSuccess(String response) { 
         //The response i get contains an additional value, lets say userkey. I want to update the right database row with that information. 
         //How to get the right rowId here? 
        } 
        }); 
    
    }  
    

    那麼實現這一目標的最佳方法是什麼?重寫AsyncHttpResponseHandler以某種方式將一個參數添加到回調函數?

  • 回答

    0

    讓您的變量最後,像

    final int rowId = <some id returned by the insert>; 
    
    0

    這是很簡單的,你可以發送ROWID。

    param.put(「_ id」,rowId);

    並且,服務器將其發送回客戶端。

    這是很好的方法,因爲它允許您僅創建一個 AsyncHttpResponseHandler實例。 (這很好實例儘可能少)

    +0

    我也實現了這一點作爲一種方式來做到這一點,但我不知道這是否是好的做法:是嗎?而且:如果我沒有訪問服務器,那麼這可能是一個問題。 – homtg

    +0

    @homtg如果你沒有aceess,你可以通過改變參數來進行檢查,比如private void addPerson(rowId,name) – ceram1

    相關問題