为什么我的代码在调试模式下工作但不能实时工作



我所拥有的是一个连接到web服务的Java代码,我试图将一些数据添加到页面。当我进行调试时,代码工作得很好,但当我试图实时运行它时,我遇到了一些问题。下面是我的Java代码:

public class PersonalInfo extends Activity{
    private GoogleMap googleMap;
    LocationManager mLocationManager;
    Geocoder geocoder;
    EditText addressnew;
     GPSTracker gps;
     boolean loginStatus;
     String Item_Name;
     String Item_Price;
     String Item_Quantity;
     String Total_Price;
     String Customer_Name;
     String Customer_Number;
     String Customer_Address;
        ArrayList<String> mixlist=new ArrayList<String>();

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.personal_info);
           final EditText name=(EditText)findViewById(R.id.edittext1);
           final EditText mobile=(EditText)findViewById(R.id.edittext2);
           addressnew=(EditText)findViewById(R.id.editText3);
           final Button locationnew=(Button)findViewById(R.id.button1);
           Button send=(Button)findViewById(R.id.button2);
      send.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            try{
                Intent newintent = getIntent();
                mixlist=newintent.getStringArrayListExtra("listmix");
                Log.e("listmix",mixlist+"");
                for(int i=0;i<=mixlist.size();i++){
                    if(i==mixlist.size()){
                         Item_Name="0";
                            Item_Price="0";
                            Item_Quantity="0";
                            Total_Price="0";
                            Customer_Name=name.getText().toString();
                            Log.e("customer_name",Customer_Name);
                            Customer_Number=mobile.getText().toString();
                            Customer_Address=addressnew.getText().toString();
                            //Call execute 
                            AsyncCallWS task = new AsyncCallWS();
                            task.execute(); 
                    }
                    else{
                Item_Name=mixlist.get(i);
                i++;
                Item_Price=mixlist.get(i);
                i++;
                Item_Quantity=mixlist.get(i);
                i++;
                Total_Price=mixlist.get(i);
                Customer_Name="0";
                Customer_Number="0";
                Customer_Address="0";
//              AsyncCallWSnew tasknew = new AsyncCallWSnew();
                //Call execute 
                AsyncCallWS task = new AsyncCallWS();
                task.execute();
                    }
                }
            }
            catch(Exception e){
                e.printStackTrace();
            }
        }
    });

//         mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

//       locationnew.setOnClickListener(new OnClickListener() {
//          
//          @Override
//          public void onClick(View v) {
//          } 
//           });
//      
//          
     } 
     @Override
     public void onBackPressed() {
        Log.d("CDA", "onBackPressed Called");
        Intent setIntent = new Intent(PersonalInfo.this,MainActivity.class);
        setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(setIntent);
     }  
     private class AsyncCallWS extends AsyncTask<Void, Void, Void> {

            protected void onPostExecute(Void result) {
                //Make Progress Bar invisible
                Toast.makeText(getApplicationContext(), "order has been sent", Toast.LENGTH_LONG).show();
                Intent intObj = new Intent(PersonalInfo.this,MainActivity.class);
                startActivity(intObj);
                //Error status is false
            }

            //Make Progress Bar visible
            protected void onPreExecute() {
            }

            @Override
            protected Void doInBackground(Void... params) {
                // TODO Auto-generated method stub
                 loginStatus = WebService.invokeLoginWS(Item_Name,Item_Price,Item_Quantity, Total_Price, Customer_Name,  
               Customer_Number, Customer_Address,"InsertData");
                return null;
            }          
        }         
}

现在,例如,mix列表是:

[orange, 2, 3, 6.0, Tomato, 0.3, 6, 1.7999999999999998]

它将添加第一个索引为项目名称,项目价格,item_quantity, total_price,然后其他值(customer-name=0, customer_number=0, customer_address=0)和执行asynctask,数组完成后,它将做相反的,我将填充(项目名称,项目价格,item_quantity, total_price) =0和其他值将从编辑文本中获取。并执行asynctask ..在调试模式下,它工作得很好。在实时中,它会像在这个数组中一样反转值,它只会添加橙色,2,3,6.0,其他三个值将重复两次!!

为什么会这样?

在for循环中创建异步任务的多个对象,其中每个对象在单独的线程上运行这是并行运行的,它可能会发生,你的第二个线程完成第一个和第一个线程完成在最后或任何顺序,这就是为什么他们不插入数据在正确的顺序,但在随机顺序。

最新更新