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

时间:2014-05-20 07:06:58

标签: android web-services android-asynctask

我所拥有的是一个与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;
            }          
        }         
}

现在例如混合列表是:

[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,其他三个值将重复两次!!

请为什么会这样?

1 个答案:

答案 0 :(得分:1)

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