-1
我需要能夠在另一個完成之後執行一個asyntask。我試圖通過在第一個方法的onPostExecute()
方法中發射第二個asyntask來做到這一點。在完成之前執行一個Asyntask
但第二ascombask沒有被解僱。但是,如果我從別處手動調用它,它會觸發它,但是因爲它試圖在第一個asyntask執行後發佈值,所以它會嘗試發送空值。
這裏是我的代碼的相關部分
//Asynctask to get Getting fb profile details
private class FetchOperation extends AsyncTask<Void, Void, String> {
String fb_token;
@Override
protected void onPreExecute() {
super.onPreExecute();
// Get user defined values
fb_token = token;
}
@Override
protected String doInBackground(Void... params) {
String response = "";
String Urls = "https://graph.facebook.com/me?access_token=";
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(Urls +token);
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
try {
httpResponse = httpclient.execute(httpget);
} catch (ClientProtocolException e) {
e.printStackTrace();
Log.v("Response", "Hi From e1 : " + e.toString());
} catch (IOException e) {
e.printStackTrace();
}
try {
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
Log.v("Response", "Hi From 2 : "+response.toString());
return response;
} catch (IOException e) {
e.printStackTrace();
Log.v("Response", "Hi From e2 : " + e.toString());
}
return null;
}
@Override
protected void onPostExecute(String jsonStr) {
super.onPostExecute(jsonStr);
Log.v("tag", "Result:\n" + jsonStr);
if (jsonStr != null) {
try{
JSONObject jsonObj = new JSONObject(jsonStr);
String email = jsonObj.getString("email");
String firstName = jsonObj.getString("first_name");
String lastName = jsonObj.getString("last_name");
String gender = jsonObj.getString("gender");
String country = jsonObj.getString("locale");
id = jsonObj.getString("id");
user = firstName.concat(" ");
user = user.concat(lastName);
image = "http://graph.facebook.com/" + id + "/picture?type=large";
Log.v("Fb name", "Testing Name : " + user);
new UploadOperation().execute();
}
catch (JSONException e) {
e.printStackTrace();
}
}
else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
}
}
這是當這個Asyntask被執行
06-29 13:06:14.707 6396-6396/com.example.kmi_dev.fbloginsample V/tag﹕ Token:
CA******************************************************************************************************G
06-29 13:06:15.427 6396-6438/com.example.kmi_dev.fbloginsample V/Response﹕ Hi From 2 : {"id":"910************","first_name":"S********","gender":"male","last_name":"Verma","link":"https:\/\/www.facebook.com\/app_scoped_user_id\/910*********\/","locale":"en_GB","name":"S******* Verma","timezone":5.5,"updated_time":"2015-06-22T04:17:39+0000","verified":true}
我做了一些周圍挖掘,並發現該onPostExecute()
在logcat中得到什麼印刷方法永遠不會被觸發,所以剩下的代碼永遠不會被執行。但我不明白爲什麼它不會被解僱!
這是第二Asyntask的代碼,這一個試圖火
//Asynctask to save fb details
private class UploadOperation extends AsyncTask<Void, Void, String> {
String api_key, user_name, img_path;
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
// Get user defined values
api_key = id;
user_name = user;
img_path = image;
//Initiate ProgressBar
dialog = ProgressDialog.show(MainActivity.this, "Please Wait", "Logging you in ...");
}
@Override
protected String doInBackground(Void... params) {
String response = "";
String Urls = "http://192.168.100.51/task_manager/v1/";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Urls +"facebookid");
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("fb_name", user_name));
nameValuePairs.add(new BasicNameValuePair("fb_id", api_key));
nameValuePairs.add(new BasicNameValuePair("fb_img_path", image));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpResponse = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
e.printStackTrace();
Log.v("Response", "Hi From e1 : " + e.toString());
} catch (IOException e) {
e.printStackTrace();
}
try {
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
Log.v("Response", "Hi From 2 : "+response.toString());
return response;
} catch (IOException e) {
e.printStackTrace();
Log.v("Response", "Hi From e2 : " + e.toString());
}
return null;
}
@Override
protected void onPostExecute(String jsonStr) {
super.onPostExecute(jsonStr);
dialog.dismiss();
Log.v("tag", "Result:\n" + jsonStr);
if (jsonStr != null) {
try{
JSONObject jsonObj = new JSONObject(jsonStr);
String message = jsonObj.getString("message");
boolean error = jsonObj.getBoolean("error");
error(error, message, api_key);
}
catch (JSONException e) {
e.printStackTrace();
}
}
else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
}
}
我想你沒有收到來自Url的迴應,或者網絡中可能有錯誤,你是否檢查過你是否得到迴應? – Kartheek
@Kartheek我收到URL的迴應等待讓我更新問題 –
@hoomi Asyntask你在說什麼? 'FetchOperation'一個?我沒有任何異常,我的logcat也不明白爲什麼我需要在第一個「錯誤」和「消息」值。能否請你用代碼 –