0

他们提到错误是在执行 doInBackground() 时发生的

public class NewClientActivity extends Activity {

// Progress Dialog
private ProgressDialog pDialog;

JSONParser jsonParser = new JSONParser();
EditText name;
EditText login;
EditText password;
EditText rePassword;
EditText email;
EditText adresse;
EditText tel;

// url to create new product
private static String url_add_client = "http://192.168.1.3/android_connect/add_client.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity1);

    // Edit Text
    name = (EditText) findViewById(R.id.textViewNom);
    login = (EditText) findViewById(R.id.textViewLogin);
    password = (EditText) findViewById(R.id.textViewPassword);
    rePassword = (EditText) findViewById(R.id.textViewPassword1);
    email = (EditText) findViewById(R.id.textViewEmail);
    adresse = (EditText) findViewById(R.id.textViewAdresse);
    tel = (EditText) findViewById(R.id.textViewTel);

    // Create button
    Button btnAddClient = (Button) findViewById(R.id.connect);

    // button click event
    btnAddClient.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // creating new product in background thread
            new CreateNewProduct().execute();
        }
    });
}

/**
 * Background Async Task to Create new product
 * */
class CreateNewProduct extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(NewClientActivity.this);
        pDialog.setMessage("Adding Customer to DataBase..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    /**
     * Creating product
     * */
    protected String doInBackground(String... args) {
        String Name = name.getText().toString();
        String Login = login.getText().toString();
        String Password = password.getText().toString();
    //  String RePassword = rePassword.getText().toString();
        String Email = email.getText().toString();
        String Adresse = adresse.getText().toString();
        String Tel = tel.getText().toString();

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("Name", Name));
        params.add(new BasicNameValuePair("Login", Login));
        params.add(new BasicNameValuePair("Password", Password));
        params.add(new BasicNameValuePair("Email", Email));
        params.add(new BasicNameValuePair("Adresse", Adresse));
        params.add(new BasicNameValuePair("Tel", Tel));


        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_add_client,
                "POST", params);

        // check log cat fro response
        Log.d("Create Response", json.toString());

        // check for success tag
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // successfully created product
            //  Intent i = new Intent(getApplicationContext(), AllProductsActivity.class);
            //  startActivity(i);

                // closing this screen
            //  finish();
            } else {
             Toast.makeText(getApplicationContext(), "L'inscription n'a pas été éfectuée correctement", Toast.LENGTH_SHORT).show();             }
        } 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 done
        pDialog.dismiss();
    }

}
}

请您检查一下并告诉我错误在哪里?先感谢您 !!

4

4 回答 4

1

我相信错误是因为您试图在 AsyncTask 的 doInBackground 方法中更新 UI。

    Toast.makeText(getApplicationContext(), "L'inscription n'a pas été éfectuée correctement", Toast.LENGTH_SHORT).show(); 

将上述代码移至 postExecute。

doInBackground 中的以下代码也不会导致发生错误

    String Name = name.getText().toString();
    String Login = login.getText().toString();
    String Password = password.getText().toString();

    String Email = email.getText().toString();
    String Adresse = adresse.getText().toString();
    String Tel = tel.getText().toString();
于 2013-05-06T11:50:03.493 回答
1

只需覆盖您AsyncTask的方法onProgressUpdate

class CreateNewProduct extends AsyncTask<String, String, String> {
    ...
    ...
    @Override
    protected void onProgressUpdate(String... values) {
       Toast.makeText(getApplicationContext(), values[0], Toast.LENGTH_SHORT).show();
    }

    /**
     * Creating product
     * */
    protected String doInBackground(String... args) {
       ...
       // check for success tag
       try {
           int success = json.getInt(TAG_SUCCESS);

           if (success == 1) {
               // successfully created product
               //  Intent i = new Intent(getApplicationContext(), AllProductsActivity.class);
               //  startActivity(i);

              // closing this screen
              //  finish();
           } else {
              publishProgress("L'inscription n'a pas été éfectuée correctement");
           }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ...
    }
}
于 2013-05-06T11:54:05.490 回答
0
Toast.makeText(getApplicationContext(), "L'inscription n'a pas été éfectuée correctement", Toast.LENGTH_SHORT).show();  

您正在尝试从后台线程更新 ui,即显示 toast。

在 onPostExecute() 中显示 toast 或使用 runonuithread

    runOnUiThread(new Runnable() //run on ui thread
        {
         public void run() 
         { 

         Toast.makeText(NewClientActivity.this,"mymessage", 1000).show();
        }
        });

使用活动上下文代替 getApplicationContext()

何时调用活动上下文或应用程序上下文?

欲了解更多信息,请查看以下链接。

http://developer.android.com/reference/android/os/AsyncTask.html

于 2013-05-06T11:45:00.220 回答
0

忘记 ASyncTask,Android Annotations 是更好的解决方案!

看这个: https ://github.com/excilys/androidannotations/wiki

于 2013-05-06T12:01:15.373 回答