ANDROID Updating a ListView via JSON with AsyncTask MySQL-Collection of common programming errors
jbihanImmersed in the development of my program, I encounter an error that seems to me not really speaking.
So here’s my logcat in the order and the java code:
When I click on the item in my listview it should return me the contact information for the update.
public class MajContactActivity extends Activity { EditText txtNom; EditText txtPrenom; EditText txtNummobile; EditText txtNumfixe; EditText txtEmail; EditText txtAdresse; EditText txtProfession; Button btnSav; Button btnSup; String idCONTACT; // Progress Dialog private ProgressDialog pDialog; // JSON parser class JSONParser jsonParser = new JSONParser(); // single contact url private static final String url_detail_contact = "http://10.0.2.2/contactCloud/detail_contact.php"; // url to update contact private static final String url_update_contact = "http://10.0.2.2/contactCloud/update_contact.php"; // url to delete contact private static final String url_delete_contact = "http://10.0.2.2/contactCloud/delete_contact.php"; // JSON Node names private static final String TAG_SUCCESS = "success"; private static final String TAG_CONTACT = "personne"; private static final String TAG_IDCONTACT = "idCONTACT"; private static final String TAG_NOM = "nom"; private static final String TAG_PRENOM = "prenom"; private static final String TAG_NUMMOBILE = "numero_mobile"; private static final String TAG_NUMFIXE = "numero_fixe"; private static final String TAG_EMAIL = "email"; private static final String TAG_ADRESSE = "adresse"; private static final String TAG_PROFESSION = "profession"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.maj_contact); // save button btnSav = (Button) findViewById(R.id.btnSav); btnSup = (Button) findViewById(R.id.btnSup); // getting contact details from intent Intent i = getIntent(); // getting contact id (idCONTACT) from intent idCONTACT = i.getStringExtra(TAG_IDCONTACT); // Getting complete contact details in background thread new GetDetailContact().execute(); // save button click event btnSav.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // starting background task to update contact new SavDetailContact().execute(); } }); // Delete button click event btnSup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // deleting contact in background thread new SupContact().execute(); } }); } /** * Background Async Task to Get complete contact details * */ class GetDetailContact extends AsyncTask { /** * Before starting background thread Show Progress Dialog * */ @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(MajContactActivity.this); pDialog.setMessage("Chargement du contact. Veuillez patientez..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } /** * Getting contact details in background thread * */ protected String doInBackground(String... params) { // Check for success tag int success; try { // Building Parameters List params1 = new ArrayList(); params1.add(new BasicNameValuePair("idCONTACT", idCONTACT)); // getting contact details by making HTTP request // Note that contact details url will use GET request JSONObject json = jsonParser.makeHttpRequest(url_detail_contact, "GET", params1); // check your log for json response Log.d("Detail contact unique", json.toString()); // json success tag success = json.getInt(TAG_SUCCESS); if (success == 1) { // successfully received contact details JSONArray personneObj = json.getJSONArray(TAG_CONTACT); // JSON Array // get first contact object from JSON Array JSONObject personne = personneObj.getJSONObject(0); // contact with this idCONTACT found // Edit Text txtNom = (EditText) findViewById(R.id.inputNom); txtPrenom = (EditText) findViewById(R.id.inputPrenom); txtNummobile = (EditText) findViewById(R.id.inputNumMobile); txtNumfixe = (EditText) findViewById(R.id.inputNumFixe); txtEmail = (EditText) findViewById(R.id.inputEmail); txtAdresse = (EditText) findViewById(R.id.inputAdresse); txtProfession = (EditText) findViewById(R.id.inputProfession); // display contact data in EditText txtNom.setText(personne.getString(TAG_NOM)); txtPrenom.setText(personne.getString(TAG_PRENOM)); txtNummobile.setText(personne.getString(TAG_NUMMOBILE)); txtNumfixe.setText(personne.getString(TAG_NUMFIXE)); txtEmail.setText(personne.getString(TAG_EMAIL)); txtAdresse.setText(personne.getString(TAG_ADRESSE)); txtProfession.setText(personne.getString(TAG_PROFESSION)); }else{ // contact with idCONTACT not found } } catch (JSONException e) { e.printStackTrace(); } return null; } /** * After completing background task Dismiss the progress dialog * **/ protected void onPostExecute(String file_url) { // dismiss the dialog once got all details pDialog.dismiss(); } } /** * Background Async Task to Save contact Details * */ class SavDetailContact extends AsyncTask { /** * Before starting background thread Show Progress Dialog * */ @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(MajContactActivity.this); pDialog.setMessage("Sauvegarde du contact ..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } /** * Saving contact * */ protected String doInBackground(String... args) { // getting updated data from EditTexts String nom = txtNom.getText().toString(); String prenom = txtPrenom.getText().toString(); String numero_mobile = txtNummobile.getText().toString(); String numero_fixe = txtNumfixe.getText().toString(); String email = txtEmail.getText().toString(); String adresse = txtAdresse.getText().toString(); String profession = txtProfession.getText().toString(); // Building Parameters List params = new ArrayList(); params.add(new BasicNameValuePair(TAG_IDCONTACT, idCONTACT)); params.add(new BasicNameValuePair(TAG_NOM, nom)); params.add(new BasicNameValuePair(TAG_PRENOM, prenom)); params.add(new BasicNameValuePair(TAG_NUMMOBILE, numero_mobile)); params.add(new BasicNameValuePair(TAG_NUMFIXE, numero_fixe)); params.add(new BasicNameValuePair(TAG_EMAIL, email)); params.add(new BasicNameValuePair(TAG_ADRESSE, adresse)); params.add(new BasicNameValuePair(TAG_PROFESSION, profession)); // sending modified data through http request // Notice that update contact url accepts POST method JSONObject json = jsonParser.makeHttpRequest(url_update_contact,"POST", params); // check json success tag try { int success = json.getInt(TAG_SUCCESS); if (success == 1) { // successfully updated Intent i = getIntent(); // send result code 100 to notify about contact update setResult(100, i); finish(); } else { // failed to update contact } } catch (JSONException e) { e.printStackTrace(); } return null; } /** * After completing background task Dismiss the progress dialog * **/ protected void onPostExecute(String file_url) { // dismiss the dialog once contact uupdated pDialog.dismiss(); } } /***************************************************************** * Background Async Task to Delete Product * */ class SupContact extends AsyncTask { /** * Before starting background thread Show Progress Dialog * */ @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(MajContactActivity.this); pDialog.setMessage("Suppression du contact..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } /** * Deleting contact * */ protected String doInBackground(String... args) { // Check for success tag int success; try { // Building Parameters List params = new ArrayList(); params.add(new BasicNameValuePair("idCONTACT", idCONTACT)); // getting contact details by making HTTP request JSONObject json = jsonParser.makeHttpRequest(url_delete_contact, "POST", params); // check your log for json response Log.d("Suppression du contact", json.toString()); // json success tag success = json.getInt(TAG_SUCCESS); if (success == 1) { // contact successfully deleted // notify previous activity by sending code 100 Intent i = getIntent(); // send result code 100 to notify about contact deletion setResult(100, i); finish(); } } catch (JSONException e) { e.printStackTrace(); } return null; } /** * After completing background task Dismiss the progress dialog * **/ protected void onPostExecute(String file_url) { // dismiss the dialog once contact deleted pDialog.dismiss(); } } } 12-05 19:46:28.287: E/AndroidRuntime(1161): FATAL EXCEPTION: AsyncTask # 212-05 19:46:28.287: E/AndroidRuntime(1161): java.lang.RuntimeException: An error occured while executing doInBackground() 12-05 19:46:28.287: E/AndroidRuntime(1161): at android.os.AsyncTask$3.done(AsyncTask.java:299) 12-05 19:46:28.287: E/AndroidRuntime(1161): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 12-05 19:46:28.287: E/AndroidRuntime(1161): at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 12-05 19:46:28.287: E/AndroidRuntime(1161): at java.util.concurrent.FutureTask.run(FutureTask.java:239) 12-05 19:46:28.287: E/AndroidRuntime(1161): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 12-05 19:46:28.287: E/AndroidRuntime(1161): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 12-05 19:46:28.287: E/AndroidRuntime(1161): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 12-05 19:46:28.287: E/AndroidRuntime(1161): at java.lang.Thread.run(Thread.java:841) 12-05 19:46:28.287: E/AndroidRuntime(1161): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 12-05 19:46:28.287: E/AndroidRuntime(1161): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:5908) 12-05 19:46:28.287: E/AndroidRuntime(1161): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:869) 12-05 19:46:28.287: E/AndroidRuntime(1161): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4253) 12-05 19:46:28.287: E/AndroidRuntime(1161): at android.view.View.invalidate(View.java:10482) 12-05 19:46:28.287: E/AndroidRuntime(1161): at android.widget.TextView.invalidateRegion(TextView.java:4591) 12-05 19:46:28.287: E/AndroidRuntime(1161): at android.widget.TextView.invalidateCursor(TextView.java:4534) 12-05 19:46:28.287: E/AndroidRuntime(1161): at android.widget.TextView.spanChange(TextView.java:7412) 12-05 19:46:28.287: E/AndroidRuntime(1161): at android.widget.TextView$ChangeWatcher.onSpanAdded(TextView.java:9103) 12-05 19:46:28.287: E/AndroidRuntime(1161): at android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:979) 12-05 19:46:28.287: E/AndroidRuntime(1161): at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:688) 12-05 19:46:28.287: E/AndroidRuntime(1161): at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:588) 12-05 19:46:28.287: E/AndroidRuntime(1161): at android.text.Selection.setSelection(Selection.java:76) 12-05 19:46:28.287: E/AndroidRuntime(1161): at android.text.Selection.setSelection(Selection.java:87) 12-05 19:46:28.287: E/AndroidRuntime(1161): at android.text.method.ArrowKeyMovementMethod.initialize(ArrowKeyMovementMethod.java:302) 12-05 19:46:28.287: E/AndroidRuntime(1161): at android.widget.TextView.setText(TextView.java:3759) 12-05 19:46:28.287: E/AndroidRuntime(1161): at android.widget.TextView.setText(TextView.java:3629) 12-05 19:46:28.287: E/AndroidRuntime(1161): at android.widget.EditText.setText(EditText.java:80) 12-05 19:46:28.287: E/AndroidRuntime(1161): at android.widget.TextView.setText(TextView.java:3604) 12-05 19:46:28.287: E/AndroidRuntime(1161): at fr.paris8.contactcloud.MajContactActivity$GetDetailContact.doInBackground(MajContactActivity.java:162) 12-05 19:46:28.287: E/AndroidRuntime(1161): at fr.paris8.contactcloud.MajContactActivity$GetDetailContact.doInBackground(MajContactActivity.java:1) 12-05 19:46:28.287: E/AndroidRuntime(1161): at android.os.AsyncTask$2.call(AsyncTask.java:287) 12-05 19:46:28.287: E/AndroidRuntime(1161): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 12-05 19:46:28.287: E/AndroidRuntime(1161): ... 4 more 12-05 19:46:36.289: E/WindowManager(1161): Activity fr.paris8.contactcloud.MajContactActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41803700 V.E..... R.....ID 0,0-480,144} that was originally added here 12-05 19:46:36.289: E/WindowManager(1161): android.view.WindowLeaked: Activity fr.paris8.contactcloud.MajContactActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41803700 V.E..... R.....ID 0,0-480,144} that was originally added here 12-05 19:46:36.289: E/WindowManager(1161): at android.view.ViewRootImpl.(ViewRootImpl.java:345) 12-05 19:46:36.289: E/WindowManager(1161): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:239) 12-05 19:46:36.289: E/WindowManager(1161): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 12-05 19:46:36.289: E/WindowManager(1161): at android.app.Dialog.show(Dialog.java:281) 12-05 19:46:36.289: E/WindowManager(1161): at fr.paris8.contactcloud.MajContactActivity$GetDetailContact.onPreExecute(MajContactActivity.java:120) 12-05 19:46:36.289: E/WindowManager(1161): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586) 12-05 19:46:36.289: E/WindowManager(1161): at android.os.AsyncTask.execute(AsyncTask.java:534) 12-05 19:46:36.289: E/WindowManager(1161): at fr.paris8.contactcloud.MajContactActivity.onCreate(MajContactActivity.java:81) 12-05 19:46:36.289: E/WindowManager(1161): at android.app.Activity.performCreate(Activity.java:5133) 12-05 19:46:36.289: E/WindowManager(1161): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 12-05 19:46:36.289: E/WindowManager(1161): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 12-05 19:46:36.289: E/WindowManager(1161): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 12-05 19:46:36.289: E/WindowManager(1161): at android.app.ActivityThread.access$600(ActivityThread.java:141) 12-05 19:46:36.289: E/WindowManager(1161): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 12-05 19:46:36.289: E/WindowManager(1161): at android.os.Handler.dispatchMessage(Handler.java:99) 12-05 19:46:36.289: E/WindowManager(1161): at android.os.Looper.loop(Looper.java:137) 12-05 19:46:36.289: E/WindowManager(1161): at android.app.ActivityThread.main(ActivityThread.java:5103) 12-05 19:46:36.289: E/WindowManager(1161): at java.lang.reflect.Method.invokeNative(Native Method) 12-05 19:46:36.289: E/WindowManager(1161): at java.lang.reflect.Method.invoke(Method.java:525) 12-05 19:46:36.289: E/WindowManager(1161): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 12-05 19:46:36.289: E/WindowManager(1161): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 12-05 19:46:36.289: E/WindowManager(1161): at dalvik.system.NativeStart.main(Native Method)
I do not see or just wrong?
thank you for the help!
PoutiThank you for your quick response,
However I did not understand, I see you’ve moved EditText in OnCreate method, and display the data of the contact in the OnPostExecute method.
I do not understand what you mean here, I must display a message
// json success tag success = json.getInt(TAG_SUCCESS); if (success == 1) { // successfully received contact details JSONArray personneObj = json.getJSONArray(TAG_CONTACT); // JSON Array // get first contact object from JSON Array JSONObject personne = personneObj.getJSONObject(0); // contact with this idCONTACT found }else{ // contact with idCONTACT not found }
Then in OnPostExecute tell me that there is a mistake, because the person is not variable defines global.
protected void onPostExecute(String file_url) { // dismiss the dialog once got all details pDialog.dismiss(); // display contact data in EditText txtNom.setText(personne.getString(TAG_NOM)); txtPrenom.setText(personne.getString(TAG_PRENOM)); txtNummobile.setText(personne.getString(TAG_NUMMOBILE)); txtNumfixe.setText(personne.getString(TAG_NUMFIXE)); txtEmail.setText(personne.getString(TAG_EMAIL)); txtAdresse.setText(personne.getString(TAG_ADRESSE)); txtProfession.setText(personne.getString(TAG_PROFESSION)); }
It’s just at this level, or I’m stuck.
Thank you very much for help.
HariharanTry this..
You cannot do the initialize inside
doInBackground
you need to with in theonCreate
. Same like that you cannot set the text inside thedoInBackground
need to inonPostExecute
.@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.maj_contact); // save button btnSav = (Button) findViewById(R.id.btnSav); btnSup = (Button) findViewById(R.id.btnSup); // Edit Text txtNom = (EditText) findViewById(R.id.inputNom); txtPrenom = (EditText) findViewById(R.id.inputPrenom); txtNummobile = (EditText) findViewById(R.id.inputNumMobile); txtNumfixe = (EditText) findViewById(R.id.inputNumFixe); txtEmail = (EditText) findViewById(R.id.inputEmail); txtAdresse = (EditText) findViewById(R.id.inputAdresse); txtProfession = (EditText) findViewById(R.id.inputProfession); // getting contact details from intent Intent i = getIntent(); // getting contact id (idCONTACT) from intent idCONTACT = i.getStringExtra(TAG_IDCONTACT); // Getting complete contact details in background thread new GetDetailContact().execute(); // save button click event btnSav.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // starting background task to update contact new SavDetailContact().execute(); } }); // Delete button click event btnSup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // deleting contact in background thread new SupContact().execute(); } }); } /** * Background Async Task to Get complete contact details * */ class GetDetailContact extends AsyncTask { /** * Before starting background thread Show Progress Dialog * */ @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(MajContactActivity.this); pDialog.setMessage("Chargement du contact. Veuillez patientez..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } /** * Getting contact details in background thread * */ protected String doInBackground(String... params) { // Check for success tag int success; try { // Building Parameters List params1 = new ArrayList(); params1.add(new BasicNameValuePair("idCONTACT", idCONTACT)); // getting contact details by making HTTP request // Note that contact details url will use GET request JSONObject json = jsonParser.makeHttpRequest(url_detail_contact, "GET", params1); // check your log for json response Log.d("Detail contact unique", json.toString()); // json success tag success = json.getInt(TAG_SUCCESS); if (success == 1) { // successfully received contact details JSONArray personneObj = json.getJSONArray(TAG_CONTACT); // JSON Array // get first contact object from JSON Array JSONObject personne = personneObj.getJSONObject(0); // contact with this idCONTACT found }else{ // contact with idCONTACT not found } } catch (JSONException e) { e.printStackTrace(); } return null; } /** * After completing background task Dismiss the progress dialog * **/ protected void onPostExecute(String file_url) { // dismiss the dialog once got all details pDialog.dismiss(); // display contact data in EditText txtNom.setText(personne.getString(TAG_NOM)); txtPrenom.setText(personne.getString(TAG_PRENOM)); txtNummobile.setText(personne.getString(TAG_NUMMOBILE)); txtNumfixe.setText(personne.getString(TAG_NUMFIXE)); txtEmail.setText(personne.getString(TAG_EMAIL)); txtAdresse.setText(personne.getString(TAG_ADRESSE)); txtProfession.setText(personne.getString(TAG_PROFESSION)); } } /** * Background Async Task to Save contact Details * */ class SavDetailContact extends AsyncTask { /** * Before starting background thread Show Progress Dialog * */ @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(MajContactActivity.this); pDialog.setMessage("Sauvegarde du contact ..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } /** * Saving contact * */ protected String doInBackground(String... args) { // getting updated data from EditTexts String nom = txtNom.getText().toString(); String prenom = txtPrenom.getText().toString(); String numero_mobile = txtNummobile.getText().toString(); String numero_fixe = txtNumfixe.getText().toString(); String email = txtEmail.getText().toString(); String adresse = txtAdresse.getText().toString(); String profession = txtProfession.getText().toString(); // Building Parameters List params = new ArrayList(); params.add(new BasicNameValuePair(TAG_IDCONTACT, idCONTACT)); params.add(new BasicNameValuePair(TAG_NOM, nom)); params.add(new BasicNameValuePair(TAG_PRENOM, prenom)); params.add(new BasicNameValuePair(TAG_NUMMOBILE, numero_mobile)); params.add(new BasicNameValuePair(TAG_NUMFIXE, numero_fixe)); params.add(new BasicNameValuePair(TAG_EMAIL, email)); params.add(new BasicNameValuePair(TAG_ADRESSE, adresse)); params.add(new BasicNameValuePair(TAG_PROFESSION, profession)); // sending modified data through http request // Notice that update contact url accepts POST method JSONObject json = jsonParser.makeHttpRequest(url_update_contact,"POST", params); // check json success tag try { int success = json.getInt(TAG_SUCCESS); } catch (JSONException e) { e.printStackTrace(); } return null; } /** * After completing background task Dismiss the progress dialog * **/ protected void onPostExecute(String file_url) { // dismiss the dialog once contact uupdated pDialog.dismiss(); if (success == 1) { // successfully updated Intent i = getIntent(); // send result code 100 to notify about contact update setResult(100, i); finish(); } else { // failed to update contact } } }
EDIT :
class GetDetailContact extends AsyncTask { /** * Before starting background thread Show Progress Dialog * */ @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(MajContactActivity.this); pDialog.setMessage("Chargement du contact. Veuillez patientez..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } /** * Getting contact details in background thread * */ protected JSONObject doInBackground(String... params) { // Check for success tag int success; try { // Building Parameters List params1 = new ArrayList(); params1.add(new BasicNameValuePair("idCONTACT", idCONTACT)); // getting contact details by making HTTP request // Note that contact details url will use GET request JSONObject json = jsonParser.makeHttpRequest(url_detail_contact, "GET", params1); // check your log for json response Log.d("Detail contact unique", json.toString()); // json success tag success = json.getInt(TAG_SUCCESS); if (success == 1) { // successfully received contact details JSONArray personneObj = json.getJSONArray(TAG_CONTACT); // JSON Array // get first contact object from JSON Array JSONObject personne = personneObj.getJSONObject(0); // contact with this idCONTACT found return personne; }else{ // contact with idCONTACT not found } } catch (JSONException e) { e.printStackTrace(); } return null; } /** * After completing background task Dismiss the progress dialog * **/ protected void onPostExecute(JSONObject personne) { // dismiss the dialog once got all details pDialog.dismiss(); // display contact data in EditText txtNom.setText(personne.getString(TAG_NOM)); txtPrenom.setText(personne.getString(TAG_PRENOM)); txtNummobile.setText(personne.getString(TAG_NUMMOBILE)); txtNumfixe.setText(personne.getString(TAG_NUMFIXE)); txtEmail.setText(personne.getString(TAG_EMAIL)); txtAdresse.setText(personne.getString(TAG_ADRESSE)); txtProfession.setText(personne.getString(TAG_PROFESSION)); } }