我有一个包含A,B和C的列表,我通过WCF将它发送到aSp.NET。 当我使用异步任务时,我得到了A,C和C,但是当我使用线程时它运行良好。
这是我的代码:
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;
你可以看到我对异步任务进行了注释。
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 :(得分:0)
我不知道你的目标是哪个版本,但我猜它是因为你没有考虑到android使用的不同的threadpoolexecutors。
试试这个:
new HttpAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "http://myseite/Service.svc/FA/SaveData");
较新版本的android默认使用线性线程池执行程序。所以asynctask一个接一个地执行。
我希望我能提供帮助。