-2

This is my code in a class that extends Asynctask and its doInBackground() method:

        //Building Parameters
        List<NameValuePair> paramss = new ArrayList<NameValuePair>();
        paramss.add(new BasicNameValuePair(TAG_EMAIL, email));

        //getting JSON string from url
        JSONObject json = parser.makeHttpRequest(url_get_one_member, "POST",paramss);

        Log.d("Ambil Member",json.toString());

        //cari tag success
        try{
            int success = json.getInt(TAG_SUCCESS);
            if(success == 1){
                member = json.getJSONArray(TAG_MEMBER);
                JSONObject jObject = member.getJSONObject(0);

                String emaill = jObject.getString(TAG_EMAIL);
                String nama = jObject.getString(TAG_NAMA);
                String alamat = jObject.getString(TAG_ALAMAT);
                String no_telp = jObject.getString(TAG_NO_TELP);
                String umur = jObject.getString(TAG_UMUR);
                String tempatLahir = jObject.getString(TAG_TEMPAT_LAHIR);
                String tglLahir = jObject.getString(TAG_TANGGAL_LAHIR);
                String jlhPeliharaan = jObject.getString(TAG_JUMLAH_PELIHARAAN);
                String warna = jObject.getString(TAG_WARNA_FAVORIT);
                String jenisKelamin = jObject.getString(TAG_JENIS_KELAMIN);
                String kota = jObject.getString(TAG_JENIS_KOTA);

                //masukin semuanya ke variabel
                txt_email.setText(emaill);
                txt_nama.setText(nama);
                txt_alamat.setText(alamat);
                txt_no_telp.setText(no_telp);
                txt_umur.setText(umur);
                txt_tempat_lahir.setText(tempatLahir);
                txt_tanggal_lahir.setText(tglLahir);
                txt_jumlah_peliharaan.setText(jlhPeliharaan);
                txt_warna_favorit.setText(warna);
                if(jenisKelamin.equals("1")){
                    txt_jenis_kelamin.setText("Pria");
                } else {
                    txt_jenis_kelamin.setText("Wanita");
                }
                txt_id_kota.setText(kota);
            }
        }catch (JSONException ex){
            ex.printStackTrace();
        }
        return null;

Sometimes it works, sometimes it doesn't.

I've checked my logcat, the json.toString() is working but I can't set all the text to the TextView.

In onCreate() I've been initializing them all:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_profile);

    //textview
    txt_email = (TextView) findViewById(R.id.txt_email);
    txt_nama = (TextView) findViewById(R.id.txt_nama);
    txt_alamat = (TextView) findViewById(R.id.txt_alamat);
    txt_no_telp = (TextView) findViewById(R.id.txt_no_telp);
    txt_umur = (TextView) findViewById(R.id.txt_umur);
    txt_tempat_lahir = (TextView) findViewById(R.id.txt_tempat_lahir);
    txt_tanggal_lahir = (TextView) findViewById(R.id.txt_tanggal_lahir);
    txt_jumlah_peliharaan = (TextView) findViewById(R.id.txt_jumlah_peliharaan);
    txt_jenis_kelamin = (TextView) findViewById(R.id.txt_jenis_kelamin);
    txt_warna_favorit = (TextView) findViewById(R.id.txt_warna_favorit);
    txt_id_kota = (TextView) findViewById(R.id.txt_id_kota);

    //button
    buttonLogout = (Button) findViewById(R.id.buttonLogout);
    buttonLogout.setOnClickListener(new android.view.View.OnClickListener() {

        @Override
        public void onClick(View v) {
            prefs = getSharedPreferences("login", Context.MODE_PRIVATE);
            editor = prefs.edit();
            editor.clear();
            editor.commit();

            Intent backToLogin = new Intent(Profile.this,Login.class);
            startActivity(backToLogin);
            finish();
        }
    });

    btnEditProfil = (Button) findViewById(R.id.btnEditProfil);
    btnEditProfil.setOnClickListener(new android.view.View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intentEdit = new Intent(getApplicationContext(),EditProfile.class);
            intentEdit.putExtra("emailMember", email);
            startActivity(intentEdit);
        }
    });
    btnPeliharaan = (Button) findViewById(R.id.btnPeliharaan);
    btnPeliharaan.setOnClickListener(new android.view.View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intentPeliharaan = new Intent(getApplicationContext(),ShowPeliharaan.class);
            startActivity(intentPeliharaan);
        }
    });

    //nagmbil email
    Intent intent = getIntent();
    email = intent.getStringExtra("emailMember");

    new LoadMember().execute();
}
shkschneider
  • 17,833
  • 13
  • 59
  • 112
  • 2
    Welkom to StackOverflow Marolop. Please always include your logcat with your post when its not clear for you whats going wrong and need help to find the error. Also mind: Stackoverflow is not used to simply post your code and let others repair it. So include the logcat to the post and try to ask if someone knows whats going wrong. **Only the error part in the logcat ofcourse** And more about the logcat you can find here: http://stackoverflow.com/questions/13857560/how-to-find-error-from-logcat-in-android – Dion Segijn Nov 28 '14 at 09:04
  • display the assigning text in post execute. It id usually to update UI.txt_email.setText(emaill); use everything in post Excecute – Shadow Nov 28 '14 at 09:06
  • What do you exspect that your code should do and what does it? What is the error? – lupz Nov 28 '14 at 09:23

1 Answers1

0

Write your AsyncTask class like this:

private class MyAsyncTask extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        List<NameValuePair> paramss = new ArrayList<NameValuePair>();
        paramss.add(new BasicNameValuePair(TAG_EMAIL, email));

        //getting JSON string from url
        JSONObject json = parser.makeHttpRequest(url_get_one_member, "POST",paramss);

        Log.d("Ambil Member",json.toString());
        return json.toString();
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        JSONObject json=new JSONObject(result);
        try{
        int success = json.getInt(TAG_SUCCESS);
        if(success == 1){
            member = json.getJSONArray(TAG_MEMBER);
            JSONObject jObject = member.getJSONObject(0);

            String emaill = jObject.getString(TAG_EMAIL);
            String nama = jObject.getString(TAG_NAMA);
            String alamat = jObject.getString(TAG_ALAMAT);
            String no_telp = jObject.getString(TAG_NO_TELP);
            String umur = jObject.getString(TAG_UMUR);
            String tempatLahir = jObject.getString(TAG_TEMPAT_LAHIR);
            String tglLahir = jObject.getString(TAG_TANGGAL_LAHIR);
            String jlhPeliharaan = jObject.getString(TAG_JUMLAH_PELIHARAAN);
            String warna = jObject.getString(TAG_WARNA_FAVORIT);
            String jenisKelamin = jObject.getString(TAG_JENIS_KELAMIN);
            String kota = jObject.getString(TAG_JENIS_KOTA);

            //masukin semuanya ke variabel
            txt_email.setText(emaill);
            txt_nama.setText(nama);
            txt_alamat.setText(alamat);
            txt_no_telp.setText(no_telp);
            txt_umur.setText(umur);
            txt_tempat_lahir.setText(tempatLahir);
            txt_tanggal_lahir.setText(tglLahir);
            txt_jumlah_peliharaan.setText(jlhPeliharaan);
            txt_warna_favorit.setText(warna);
            if(jenisKelamin.equals("1")){
                txt_jenis_kelamin.setText("Pria");
            } else {
                txt_jenis_kelamin.setText("Wanita");
            }
            txt_id_kota.setText(kota);
        }
        }catch (JSONException ex){
         ex.printStackTrace();
      }
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
    }

}