2013-07-23 55 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();