android - AsyncTask 类中的错误无法发送数据

标签 android android-asynctask

我在 android 平台上工作,想将一些注册详细信息发送到 php 页面,为此我创建了一个名为“ContinueRegister”的 Activity ,它包含一个 AsyncTask 类。在运行我的项目时,它显示不幸的是已经停止并且 logcat 中有一些错误,比如

 Activity com.opz.ContinueRegister has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41269db8     
        error opening trace file: No such file or directory
        at android.os.Looper.loop(Looper.java:137)
        at android.os.Handler.handleCallback(Handler.java:615)
        atcom.opz.ContinueRegister$CreateNewUser.onPreExecute(ContinueRegister.java:76)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
        at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
        at java.lang.reflect.Method.invokeNative(Native Method)

这是我的类(class)文件

public class ContinueRegister extends Activity {

    // Progress Dialog
        private ProgressDialog pDialog;

        JSONParser jsonParser = new JSONParser();

    EditText firstname;
    EditText lastname;
    EditText dob;
    EditText gender;

    RegisterActivity ra= new RegisterActivity();

// url to create new product
    private static String url_create_product = "http://localhost/login_api/create_account.php/";

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

   @Override    
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
        // Set View to register.xml
       setContentView(R.layout.registerfinsh);

             Button bt=(Button)findViewById(R.id.btnRegister);

       // Listening to Login Screen link
       bt.setOnClickListener(new View.OnClickListener(){

            public void onClick(View arg0) {

                    new CreateNewUser().execute();
                    }
            });

            }


            /**
     * Background Async Task to Create new product
     * */

class CreateNewUser extends AsyncTask<String, String, String> {
/**
 * Before starting background thread Show Progress Dialog
 * */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ContinueRegister.this);
pDialog.setMessage("Creating New User..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}

/**
 * Creating User
 * */
protected String doInBackground(String... args) {

     String Username = ra.username.getText().toString();
     String Email = ra.email.getText().toString();
     String Password =ra.password.getText().toString();
     firstname = (EditText)findViewById(R.id.first_name);
     lastname =(EditText)findViewById(R.id.last_name);
     dob =(EditText)findViewById(R.id.date_of_birth);
     gender = (EditText)findViewById(R.id.gender);
     String Firstname = firstname.getText().toString();
     String Lastname = lastname.getText().toString();
     String Dob =dob.getText().toString();
     String Gender =gender.getText().toString();

            // Building Parameters

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("Username", Username));
            params.add(new BasicNameValuePair("Email", Email));
            params.add(new BasicNameValuePair("Password", Password));
            params.add(new BasicNameValuePair("Firstname", Firstname));
            params.add(new BasicNameValuePair("Lastname", Lastname));
            params.add(new BasicNameValuePair("Dob", Dob));
            params.add(new BasicNameValuePair("Gender", Gender));

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

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

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

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

                    // closing this screen
                    finish();
                } else {
                    // failed to create user
                }
            } 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();


       TextView loginScreen = (TextView) findViewById(R.id.link_to_login2);

       loginScreen.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent m = new Intent(getApplicationContext(),LoginActivity.class);
            startActivity(m);

        }
    });
   }
 }
}

请帮我解决这个错误

最佳答案

String url_create_product = "http://10.0.2.2/login_api/create_account.php/";

使用您的本地 IP,如 192.168.0.1

String url_create_product = "http://192.168.0.1/login_api/create_account.php/";

同时检查您的 login_api 文件夹中是否有名为 create_account.php 的文件

还有一件事是永远不要访问 doinBackGround(...) 中的任何 View

因为doinBackGround方法是非UI线程。

关于android - AsyncTask 类中的错误无法发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12423497/

相关文章:

android - 从 AsyncTaskLoader 更新 UI

android - java.lang.IllegalStateException : YouTubeServiceEntity not initialized with YouTubeThumbnailView 错误

java - 使用自定义 View 设置布局

Android Realm 迭代器异常

python - Kivy文本输入: Chinese characters

android - IllegalStateException fragment 在旋转后未附加到 onPostExecute 中的 Activity ,但未调用 onDetach

android - setText 无法在 TextView 中将数字显示为文本

android - 当我滚动 gridview 时图像发生变化

Android - 网络数据操作期间如何处理来电

android - 具有 Intent 服务的 AsyncTask(带 AlarmManager)