-2
有人可以請解釋爲什麼變量picture
的值僅存在於OnInBackground()
?變量值無法從DoInBackground()中獲取
當我在OnPostExecute
中訪問它時,數值會丟失。
public class GetPostAsync extends AsyncTask<Void, Integer, String>
{
String picture="";
public GetPostAsync(Context context){
c=context;
}
protected void onPreExecute(){
}
protected String doInBackground(Void...arg0) {
try {
GraphRequest request = GraphRequest.newGraphPathRequest(
AccessToken.getCurrentAccessToken(),
"/myquery",
new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
try {
JSONObject jobj = response.getJSONObject();
picture = jobj.getString("picture");
Log.i("TEST","pic: "+picture); //return the right value
} catch (JSONException e) {
e.printStackTrace();
}
}
});
request.executeAsync();
}
catch(Exception e) {
}
return picture;
}
protected void onProgressUpdate(Integer...a){
}
protected void onPostExecute(String result) {
Log.i("TEST","pic: "+picture); //return empty
}
}
我已在活動的頂部聲明picture
。我不知道如何保留我在Onbackground()
您不必在'doInBackground'中返回值,因爲它已經在內部類的全局變量中。把這個'AsyncTask'改成這個'AsyncTask ' –
MohammedAlSafwan
@MohammedAlSafwanplease瞭解一些java的基礎知識......它不是全局變量,而是字段 – Selvin
它是多線程...請求。 executeAsync(); http://ideone.com/PPHi95 – Selvin