0

我正在尝试从 FB 获取记分牌,并为此向 API 发出 HTTP 请求。它必须在一个单独的线程上完成,这是我的异步任务类:

public class AsyncBuildScoreboard extends AsyncTask<Void, Void,String> {
ProgressBar pb;

@Override
protected void onPreExecute() {
    super.onPreExecute();

}

@Override
protected String doInBackground(Void... voids) {

    String token = Session.getActiveSession().getAccessToken();     
    try{            
        HttpClient client = new DefaultHttpClient();                
        HttpGet get = new HttpGet("https://graph.facebook.com/"+GameStatic.app_id+"?fields=scores&access_token=" + token);
        HttpResponse resp = client.execute(get);                    
        HttpEntity responseEntity = resp.getEntity();
        String response = EntityUtils.toString(responseEntity);     
        return response;

    }
    catch (IOException e) 
    {

    }   
    return "";

}


protected void onPostExecute(String response) {
    try{    
        JSONObject res = new JSONObject(response);                          
        JSONObject scores = res.getJSONObject("scores");
        JSONArray data = scores.getJSONArray("data");                       
        int len = data.length();                        
        String[] values = new String[len];
        for(int i=0;i<len;i++)
        {
            JSONObject obj = data.getJSONObject(i);                         
            JSONObject user = obj.getJSONObject("user");
            String name = user.getString("name");
            String score = obj.getString("score");          
            values[i] = name + "  " + score;
            GameStatic.scoreboard.add(values[i]);
        }
    }       
    catch (JSONException e)
    {

    }



}

}

GameStatic 是一个外部变量,用于存储我从线程中获得的内容。然而,当这样做时:

AsyncBuildScoreboard board = new AsyncBuildScoreboard ();
    board.execute();        
    final ListView listview = (ListView) findViewById(R.id.scorelist);            
    final StableArrayAdapter adapter = new StableArrayAdapter(this,
                android.R.layout.simple_list_item_1, GameStatic.scoreboard);        
    listview.setAdapter(adapter); 

发生空指针异常,这意味着 GameStatic.scoreboard 尚未填充我想要的条目。

我做错了什么,有什么想法吗?

我有义务,因为我真的按时...

public class AsyncBuildScoreboard extends AsyncTask<Void, Void, Void> 
{
public ListView list;
public Context ctx;
public ArrayList<String> scoreboard = new ArrayList <String> ();


public AsyncBuildScoreboard(ListView list, Context context)
{
    this.list = list;
    this.ctx = context;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();

}

@Override
protected Void doInBackground(Void... voids) 
{
    Void nothing = null;
    String token = Session.getActiveSession().getAccessToken();     
    try{            
        HttpClient client = new DefaultHttpClient();                
        HttpGet get = new HttpGet("https://graph.facebook.com/"+GameStatic.app_id+"?fields=scores&access_token=" + token);
        HttpResponse resp = client.execute(get);                    
        HttpEntity responseEntity = resp.getEntity();
        String response = EntityUtils.toString(responseEntity); 


    }
    catch (IOException e) 
    {

    }   
    return nothing;     
}


protected void onPostExecute(String response) {
    try{    
        JSONObject res = new JSONObject(response);                          
        JSONObject scores = res.getJSONObject("scores");
        JSONArray data = scores.getJSONArray("data");                       
        int len = data.length();                        
        String[] values = new String[len];
        for(int i=0;i<len;i++)
        {
            JSONObject obj = data.getJSONObject(i);                         
            JSONObject user = obj.getJSONObject("user");
            String name = user.getString("name");
            String score = obj.getString("score");          
            values[i] = name + "  " + score;
            scoreboard.add(values[i]);
        }
    }       
    catch (JSONException e)
    {

    }
    final ArrayAdapter adapter = new ArrayAdapter(ctx,
            android.R.layout.simple_list_item_1, scoreboard);       
    list.setAdapter(adapter); 


}



}
4

1 回答 1

1

您将需要用于在执行完成onPostExecute时显示分数,而不是在之后通过,因为总是在单独的线程中执行,所以它。doInBackgroundGameStatic.scoreboardAsyncTask.execute()doInBackground

您可以使用 AsyncBuildScoreboard类构造函数从 Activity 传递 ListView 实例和上下文:

ListView listview;
Context context;
public AsyncBuildScoreboard(ListView listview,Context context){
 this.context=context;
 this.listview=listview;
}
....
protected void onPostExecute(String response) {
   //...your code here..

  StableArrayAdapter adapter = new StableArrayAdapter(context,
             android.R.layout.simple_list_item_1, GameStatic.scoreboard);        
    listview.setAdapter(adapter); 
}

并更改您的 Activity 代码以传递 Context :

ListView listview = (ListView) findViewById(R.id.scorelist);
AsyncBuildScoreboard board = new AsyncBuildScoreboard (listview,
                                                     Your_Activity.this);
board.execute(); 

或者您也可以使用在 doInBackground 执行完成时在 UI 线程上触发的接口创建回调方法。有关详细信息,请参阅以下帖子:

android asynctask向ui发送回调

于 2013-06-04T20:21:30.747 回答