2014-08-27 76 views
-3

我得到一個包含A,B和C的列表,並且我通過WCF將它發送到Sp.NET。 當我使用async任務時,我得到了A,C和C,但是當我使用線程時它工作得很好。爲什麼Android線程工作和異步不起作用

這裏是我的代碼:

case R.id.btn_Reception: 
      {    
       for(Pers_Ordre p : ListOrdres){    
        dbcon.open(); 

        p.setLe_Statut("1"); 
        p.setLe_Camion(sharePref_Camion.getString("CammionSetting","")); 

        ordreItem = p; 

        dbcon.createItmes(p);     
        dbcon.close(); 

        // call AsynTask to perform network operation on separate thread 

        // new HttpAsyncTask().execute("http://myseite/Service.svc/FA/SaveData"); 

         new Thread(new Runnable() { 
          public void run() { 
           runOnUiThread(new Runnable() { 
             public void run() { 

             } 
            });      

           POST("http://myseite/Service.svc/FA/SaveData", ordreItem); 

          } 
          }).start(); 
       } 

        ListOrdres.clear(); 
        m_adapter = new CustomListAdapter(this, ListOrdres); 
        m_adapter.notifyDataSetChanged(); 
        lv_Ordre.setAdapter(m_adapter); 

       Toast.makeText(getApplicationContext(), 
         "Rec", Toast.LENGTH_SHORT).show(); 
      } 
      break; 

,你可以看到我comented異步任務。

EDITED

這裏是我的異步代碼:

private class HttpAsyncTask extends AsyncTask<String, Void, String> { 
    @Override 
    protected String doInBackground(String... urls) {   
       return POST(urls[0],ordreItem);    
    } 
    // onPostExecute displays the results of the AsyncTask. 
    @Override 
    protected void onPostExecute(String result) { 
     if (result.isEmpty())result ="Oke"; 
     //else result = "Doublons"; 
     Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show(); 
     Log.d("ERR_WCF", result); 
    } 
} 

public static String POST(String url, Pers_Ordre TheOrdre){ 
    InputStream inputStream = null; 
    String result = ""; 
    try { 

     // 1. create HttpClient 
     HttpClient httpclient = new DefaultHttpClient(); 

     // 2. make POST request to the given URL 
     HttpPost httpPost = new HttpPost(url); 

     String json = ""; 

     // 3. build jsonObject 
     JSONObject jsonObject = new JSONObject();   

     jsonObject.accumulate("CodeClient", TheOrdre.getLe_CodeClient()); 
     jsonObject.accumulate("CodeDest", TheOrdre.getLe_CodeDest()); 
     jsonObject.accumulate("NoOrdre", TheOrdre.getLe_NoOrdre()); 
     jsonObject.accumulate("LeDate", TheOrdre.getLe_Date()); 
     jsonObject.accumulate("LeGPS", TheOrdre.getLe_GPS());    
     jsonObject.accumulate("LeStatut", TheOrdre.getLe_Statut()); 
     jsonObject.accumulate("LeCamion", TheOrdre.getLe_Camion()); 
     jsonObject.accumulate("LeOrdrePos", TheOrdre.getLe_Ordre_1()); 
     jsonObject.accumulate("LeOrdreTot", TheOrdre.getLe_Ordre_2()); 


     // 4. convert JSONObject to JSON to String 
     json = jsonObject.toString(); 

     // ** Alternative way to convert Person object to JSON string usin Jackson Lib 
     // ObjectMapper mapper = new ObjectMapper(); 
     // json = mapper.writeValueAsString(person); 

     // 5. set json to StringEntity 
     StringEntity se = new StringEntity(json); 

     // 6. set httpPost Entity 
     httpPost.setEntity(se); 

     // 7. Set some headers to inform server about the type of the content 
     httpPost.setHeader("Accept", "application/json"); 
     httpPost.setHeader("Content-type", "application/json"); 

     // 8. Execute POST request to the given URL 
     HttpResponse httpResponse = httpclient.execute(httpPost); 

     // 9. receive response as inputStream 
     inputStream = httpResponse.getEntity().getContent(); 


     // 10. convert inputstream to string 
     if(inputStream != null) 
      result = convertInputStreamToString(inputStream); 
     else 
      result = "Did not work!"; 

    } catch (Exception e) { 
     Log.d("InputStream", e.getLocalizedMessage()); 
    } 

    // 11. return result 
    return result +TheOrdre.getLe_CodeClient()+"__"+TheOrdre.getLe_CodeDest()+"__"+TheOrdre.getLe_NoOrdre()+ "__"+TheOrdre.getLe_Ordre_1()+"--"+TheOrdre.getLe_Ordre_2(); 
} 
+0

不知道你的HttpAsyncTask在做什麼很困難。 – dm78 2014-08-27 12:48:56

回答

0

我不知道你是針對哪個版本,但我猜測那是因爲你沒有考慮不同threadpoolexecutors那android使用。

試試這個:機器人的

new HttpAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "http://myseite/Service.svc/FA/SaveData"); 

新版本默認使用線性線程池執行。所以asynctask被一個接一個地執行。

我希望我可以幫忙。