2013-07-23 123 views
0

我正在Intentservice的onHandleIntent()方法內部運行一個簡單的fql查詢。不知道問題是什麼,查詢部分沒有被執行。這裏是我的onHandleIntent()方法Android IntentService問題

protected void onHandleIntent(Intent intent) 
{ 

    System.out.println("inside service method now"); 
    String fqlQuery = "SELECT uid, username, online_presence, status FROM user WHERE uid = 100001024732884"; 

     Bundle params = new Bundle(); 
     params.putString("q", fqlQuery); 
     Session session = Session.getActiveSession(); 
    Request request = new Request(session, 
       "/fql",       
       params,       
       HttpMethod.GET,     
       new Request.Callback(){   
        public void onCompleted(Response response) { 
        System.out.println("inside the service now 4444"); 
         Log.i(TAG, "Result: " + response.toString()); 
        }     
      }); 
      Request.executeBatchAsync(request); 
} 

只打印第一條語句。

當我把我的MainActivity相同的代碼它工作正常,但在IntentService它停止工作。我正在按鈕上啓動服務。

+0

您必須嘗試先發現錯誤。這個方法必須拋出一些錯誤。使用try catch方法並捕獲錯誤。那麼你可能可以解決這個問題。 –

+0

你的意思是隻有'現在服務4444'內部打印? – gunar

+0

不,只有第一個打印語句正在執行。 – user1872750

回答

0

你的問題是IntentService將在你的回調被調用之前完成並銷燬,因爲它是異步運行的。而不是使用回調來獲取響應,請嘗試使用它。

Bundle params = new Bundle(); 
    params.putString("q", fqlQuery); 
    Session session = Session.getActiveSession(); 
    Request request = new Request(session, 
      "/fql",       
      params,       
      HttpMethod.GET); 
    Response response = request.executeBatchAndWait();