0

我正在 IntentService 中执行一个简单的查询(fql)(通过单击按钮开始)。这是我的服务类的 onHandleIntent() 方法:

 protected void onHandleIntent(Intent intent)
{
    Global appState = ((Global)getApplicationContext());
    System.out.println("inside the service now");   
    String uid=appState.uid[0];
    System.out.println("uid ="+uid);

    String fqlQuery = "SELECT uid, name, online_presence, status FROM user WHERE uid="+uid+" ";
    Bundle params = new Bundle();
    params.putString("q", fqlQuery);
    Session session = Session.getActiveSession();
    System.out.println(session);
        Request request = new Request(session,                                  // starting of innerclass
        "/fql",                         
         params,                         
         HttpMethod.GET,                 
         new Request.Callback(){  

         public void onCompleted(Response response) {
              //Log.i(TAG, "Result: " +response.toString() );                        //response.toString()
         GraphObject graphObject = response.getGraphObject();
            // String s = textViewResults.getText().toString();
          if (graphObject != null) {
                    JSONObject jsonObject = graphObject.getInnerJSONObject();
                       try {
                         JSONArray array = jsonObject.getJSONArray("data");
                            for (int j = 0; j < array.length(); j++) {                     // always array.length()=1, loop will run jst 1 time
                             JSONObject object = (JSONObject) array.get(j);
                                System.out.println("inside inner class now");
                                 System.out.println("id = " +object.get("uid").toString() );
                                 System.out.println("name = "+object.get("name").toString());
                                 System.out.println("op ="+object.get("online_presence").toString()+"niraj");
                                 name=object.get("name").toString();
                             }
                 } catch (JSONException e) {

                  e.printStackTrace();
                 }
          }          // end of if block


              }  // end of onCompleted method                  
      }); 

         // Thread.currentThread().sleep(10000);//sleep for 1000 ms
  Request.executeBatchAsync(request); 


  System.out.println("outsise the inner class ____ name : "+name); 

}

问题是内部类中的代码段(从请求开始)没有被执行。对于上面的代码,我得到以下输出:

07-26 17:21:09.105: I/System.out(1541): inside the service now
07-26 17:21:09.105: I/System.out(1541): uid =100002621905500
07-26 17:21:09.136: I/System.out(1541): {Session state:OPENED, token:{AccessToken    
token:ACCESS_TOKEN_REMOVED permissions:[user_likes, user_status,    
friends_online_presence, user_online_presence]}, appId:"my app id"}
07-26 17:21:09.206: I/System.out(1541): outsise the inner class ____ name : null

我没有得到错误是什么。甚至日志也没有显示任何错误。请帮忙。

4

2 回答 2

0

使用
Request.executeBatchAndWait(request); 代替 Request.executeBatchAsync(request);

解决了这个问题,因为“Request.executeBatchAndWait(request)”不需要回调,它接收请求并在同一个线程中处理它,而“Request.executeBatchAsync(request)”在不同的线程中处理请求结果,因此,为了处理结果或确定过程是成功还是失败,指定了回调。

于 2013-07-27T21:19:00.727 回答
0

很难说,因为我没有看到 Request 类型的定义,但我怀疑你遇到了问题,因为你期待 IntentService 中的回调。那是行不通的。IntentService 会一直运行,直到 onHandleIntent() 中的所有代码都完成,然后将其销毁。如果某些东西试图进行回调,它将无法工作,因为它找不到实例。

您可能需要改用已启动的服务。

于 2013-07-26T23:10:16.670 回答