I have read many question like my one on stackoverflow but I have not found any solution for my problem. The problem is that I call the AsynkTask
with the .execute()
method in a onClick. The doInBackground
completes but after that the onPostExecute
is not called, even if there is @override
on it and doInBackground
returns a value. I post is the class.
public class FragmentControllaCorsa extends Fragment implements View.OnClickListener{
private Button controlla;
private TextView ultimaCorsa, validitaCorsa;
private EditText codiceDocumento;
private Context context;
// Tag usato nei log.
// Tag used into logs.
private static final String TAG = FragmentControllaCorsa.class.getName();
// Test dall'Emulatore:
// Testing from Emulator:
private static final String LOGIN_URL = "http://10.0.2.2/PrimaAppAndroid/get/ultimaTratta.php";
// Tag(ID) del responso Json restituito dallo script php:
// JSON element ids from repsonse of php script:
private static final String TAG_SUCCESS = "success";
private static final String TAG_DATA = "Data";
private static final String TAG_DURATA = "DurataConvalida";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/*
Inserisco il layout di questo fragment.
--
Inflate the layout for this fragment
*/
return inflater.inflate(R.layout.fragment_controlla_corsa, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
/*
Creo Il bottone controlla e vi associo come listener questa classe stessa.
--
I create the button controlla and I associate him this class as listnener.
*/
controlla = (Button) getView().findViewById(R.id.buttonControllaCorsa);
controlla.setOnClickListener(this);
/*
Ottengo le due edit text dal file .xml, il context dell'activity e l'EditText codiceDocumento.
--
I get the two EditText from the .xml file, the activity context and the EditText codiceDocumento.
*/
ultimaCorsa = (TextView) getView().findViewById(R.id.ultimaCorsaControlloCorsa);
validitaCorsa = (TextView) getView().findViewById(R.id.validitaCorsaControlloCorsa);
context = getActivity();
codiceDocumento = (EditText) getView().findViewById(R.id.codiceDocumentoControllaCorsa);
}
@Override
public void onClick(View view) {
new DownloadUltimaCorsa().execute();
}
class DownloadUltimaCorsa extends AsyncTask<Void, Void, JSONObject>{
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setMessage("Download ultima tratta...");
// Dura un tempo indeterminato.
// It lasts a undefined time.
pDialog.setIndeterminate(false);
// E' cancellabile dal tasto Back.
// It's cancelable by the Back key.
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(Void... voids) {
// Per il controllo se è andato a buon fine.
// Checking if all goes well.
int success;
// Il parametro da passare.
// The param to pass.
String codice = codiceDocumento.getText().toString();
JSONParser jsonParser = new JSONParser();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>(); //http://stackoverflow.com/a/17609232/2337094 Namevaluepair
params.add(new BasicNameValuePair("codice", codice));
// Avvio della richiesta.
Log.d("request!", "starting");
// Start the request.
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
// Controllo del respondo json
// Check your log for json response
Log.d("Download effettuato", json.toString());
return json;
// json success tag
/*success = json.getInt(TAG_SUCCESS);
if (success == 1) {//SE VORRÒ GESTIRE L'ERRORE.
return json;
} else {
return json;
}*/
}
@Override
protected void onPostExecute(JSONObject json) {
super.onPostExecute(json);
// Chiudo il ProgressDialog.
// Dismiss the ProgressDialog.
pDialog.dismiss();
try {
JSONArray valore = new JSONArray(json.toString());
JSONObject riga = valore.getJSONObject(0);
ultimaCorsa.setText(riga.getString(TAG_DATA));
validitaCorsa.setText(riga.getString(TAG_DURATA));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
Why onPostExecute() is not called? thank you in advance!