我一直在研究一个完全依赖于本地服务器上数据库信息的android应用程序(信息以来自web服务的json数组的形式出现)。
所以我一直在使用多个AsyncTasks,我将在OnCreate中执行第一个,而第二个将在第一个的OnPostExecute中,因此链接。 我的问题是什么是最好的做法?我的意思是让一个AsyncTask执行我所有的http get / post请求会更好吗? 这是我的代码示例: 第一个http AsyncTask的代码:
public class httpAddProduct extends AsyncTask<Void,Void,String> {
String rs;
@Override
protected void onPreExecute() {
super.onPreExecute();
dd_dialog=showProgressDialog();
}
@Override
protected String doInBackground(Void... params) {
try {
String proID=selectedCatProID.get(itemIndex);
String link = "http://"+IP+"/H2O.asmx/Add_Product?OrderID="+orderId+"&ItemNo="+proID
+"&Qty="+Qyt;
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(link));
client.execute(request);
addPro=true;
isChanged=true;
rs = "sucessful";
} catch (Exception e) {
rs = "Fail";
}
return rs;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(s.equals("sucessful")){
new httpGetOrder().execute();
dd_dialog.hide();
}}
}
这是第二个http代码:
public class httpGetOrder extends AsyncTask<Void,Void,String> {
String result, rs;
JSONArray jArray;
Boolean isEmpty=true;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(Void... params) {
try {
String link = "http://"+IP+"/H2O.asmx/Get_Order?OrderID="+orderId;
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(link));
HttpResponse response = client.execute(request);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
String[] separated = result.split(">");
JSONObject jObject = new JSONObject(separated[2]);
jArray = jObject.getJSONArray("OrderItems");
lineIDs = new ArrayList<>();
productNames = new ArrayList<>();
QYTs = new ArrayList<>();
unitPrice = new ArrayList<>();
subtotal = new ArrayList<>();
FatherCateg=new ArrayList<>();
isPrinted1=new ArrayList<>();
isPrinted2=new ArrayList<>();
isPrinted3=new ArrayList<>();
note=new ArrayList<>();
isEmpty=true;
int jlen=jArray.length();
if(jlen>0) {
for (int i = 0; i < jlen; i++) {
if(jArray.getJSONObject(i)!=null) {
JSONObject json_data = jArray.getJSONObject(i);
lineIDs.add(json_data.getString("LineID"));
productNames.add(json_data.getString("En_Name"));
unitPrice.add(json_data.getString("UnitPrice"));
subtotal.add(json_data.getString("SubTotal"));
QYTs.add(json_data.getString("Qty"));
FatherCateg.add(json_data.getString("Father"));
note.add(json_data.getString("LineNote"));
isPrinted1.add(json_data.getString("IsPrinted"));
isPrinted2.add(json_data.getString("IsPrinted2"));
isPrinted3.add(json_data.getString("IsPrinted3"));
}
}
isEmpty=false;
}
rs = "sucessful";
} catch (Exception e) {
rs = e.getMessage();
}
return rs;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(s.equals("sucessful")){
try{
if(isEmpty){
grandt.setText("0");
invoiceList.setAdapter(null);
dd_dialog.hide();
}else{
makeInvoice();
listviewAdapter adapter = new listviewAdapter(SelectCategory.this, list);
invoiceList.setAdapter(adapter);
if(addPro)
{invoiceList.setSelection(adapter.getCount()-1); addPro=false;}
else
invoiceList.setSelection(selectionIndex);
new httpGetTotal().execute();
}}
catch (Exception e){
Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
}}
}
}
然而这激发了第三个AsyncTask。 我知道代码太长了:/抱歉 但我的应用越来越慢,我需要提出这个问题的建议。