我正在研究一些代碼,它將從Facebook中提取JSON信息並將其放入EditText視圖中。我遇到的問題是因爲我調用來檢索數據的函數是一個異步函數,在我的活動的onCreate函數結束後正在檢索數據,所以文本無法更新,因爲當它爲空。 我怎樣才能確保我的OnCreate方法不會結束,直到數據被檢索?OnCreate在可以異步檢索數據之前結束
這裏是我的活動課:
public class PersonalInfoMain extends AppCompatActivity implements GraphService.OnDownloadComplete {
public EditText mName;
public String name;
String mValue, mField;
private static final String TAG = "PersonalInfoMain";
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate: starts");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_personal_info_main);
mName = (EditText) findViewById(R.id.editText_name);
//Calling GraphService class
GraphService mGraphService = new GraphService(this);
mField = "name";
mGraphService.execute(mField);
mName.setText(name);
}
}
//My Callback function located in the same activity:
@Override
public void onDownloadComplete(String s) {
name = s;
}
這裏是我的I類標記GraphService從Facebook的拉動信息:
public class GraphService extends AsyncTask<String, Void, String>{
private static final String TAG = "GraphService";
private String mField;
private String mValue;
private final OnDownloadComplete mCallback;
public interface OnDownloadComplete{
void onDownloadComplete(String data);
}
public GraphService(OnDownloadComplete callback){
mCallback = callback;
}
@Override
public String doInBackground(final String... params) {
Log.d(TAG, "doInBackground: starts");
mField = params[0];
//Beginning of Facebook implementation
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
mValue = object.getString(mField);
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
);
Bundle parameters = new Bundle();
parameters.putString("fields", mField);
request.setParameters(parameters);
request.executeAsync();
return mValue;
}
@Override
protected void onPostExecute(String s) {
if(mCallback != null){
mCallback.onDownloadComplete(s);
}
}
我的Android監視器顯示該工作流程:
D/MainActivity: onCreate: starts
D/MainActivity: isLoggedIn: starts
D/MainActivity: isLoggedIn: ends
D/MainActivity: onCreate: ends
D/PersonalInfoMain: onCreate: starts
D/PersonalInfoMain: onCreate: name = null
D/GraphService: doInBackground: starts
D/PersonalInfoMain: onCreate: ends
D/PersonalInfoMain: onResume: starts
D/PersonalInfoMain: onResume: name = null
D/GraphService: doInBackground: ends
D/PersonalInfoMain: onResume: ends
D/GraphService: onPostExecute: starts
D/PersonalInfoMain: onDownloadComplete: starts
D/PersonalInfoMain: onDownloadComplete: ends
D/GraphService: onPostExecute: ends
D/GraphService: onCompleted: starts, JSON object = {"name":"Tom"}, response = {"name":"Tom"}
D/GraphService: onCompleted: json object:{"name":"Tom"}
D/GraphService: onCompleted: ends
正如您所看到的,我的活動中的.setText()方法將其設置爲由於Async任務(onCompleted()方法)尚未返回數據,因此爲空值。 我怎樣才能確保我的OnCreate方法不會結束,直到數據被檢索?
我將不勝感激任何意見。
謝謝。
嗨Sunil,我總是聽說像網絡請求這樣的短操作應該異步調用。你能解釋爲什麼我不應該這樣做嗎? 我試過了你的代碼,它仍然不更新mName,因爲在onDownloadComplete函數結束之後調用了JSON檢索代碼(正如你在Logcat中看到的那樣) – Ozzy
是的,我剛剛發現你又一次異步GraphRequest在doInBackground中,所以在這種情況下,即使在從請求中獲得結果之前,也可以調用return。在這裏你不需要AsyncTask,因爲你可以直接調用GraphRequest。 –