我正在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);
}
问题是内部类中的代码段(从Request开始)不会仅执行。对于上面的代码,我得到以下输出:
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
我没有得到错误的是。即使日志没有显示任何错误。请帮助。
答案 0 :(得分:0)
很难说,因为我没有看到Request类型的定义,但是我怀疑你遇到了问题,因为你期望在你的IntentService中进行回调。那不行。 IntentService一直运行,直到onHandleIntent()中的所有代码都完成,然后被销毁。如果某些东西试图进行回调,它将无法工作,因为它找不到实例。
您可能需要使用已启动的服务。
答案 1 :(得分:0)
Request.executeBatchAndWait(请求); 原位的 Request.executeBatchAsync(请求);
解决问题,因为“Request.executeBatchAndWait(request)”不需要回调,它接收请求并在同一个线程中处理它,而“Request.executeBatchAsync(request)”处理请求结果不同的线程,所以为了处理结果或确定进程是成功还是失败,指定了回调。