我遇到了一个奇怪的问题。我以前从未见过。在代码中,执行了 asynctask,在 doInBackground 中我调用了一个函数,但函数没有执行。问题是什么 ?我没有收到任何错误消息,只是函数没有执行并item_details
返回 null。
注意:在 asynctask 中,int x
value 不为 null。
这是异步任务
public class GetItemDetailAS extends AsyncTask<String,String[],String[]>{
String ParamID;
private ProgressDialog dialog = new ProgressDialog(ItemActivity.this);
public GetItemDetailAS(String ParamID){
this.ParamID=ParamID;
}
@Override
protected void onPreExecute() {
dialog.setMessage("Loading...");
dialog.show();
}
@Override
protected String[] doInBackground(String... params) {
int x = Integer.valueOf(ParamID);
GetItemDetail(x);
return item_details;
}
protected void onPostExecute(String[] item_details){
_title.setText(item_details[5]);
_sellerName.setText(item_details[1]);
_location.setText(item_details[6]);
_price.setText(item_details[3]);
_desc.setText(item_details[4]);
if(item_details[7].toString().length()<20){
_picture.setImageResource(R.drawable.desire_z);
}
else{
byte[] decodedString = Base64.decode(item_details[7], Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
_picture.setImageBitmap(decodedByte);
}
dialog.dismiss();
}
}
这是功能
private String[] GetItemDetail(int paramID){
PropertyInfo id = new PropertyInfo();
id.name= "itemid";
id.setValue(paramID);
id.type = PropertyInfo.INTEGER_CLASS;
SoapObject request = new SoapObject("http://tempuri.org/", "GetItem");
request.addProperty(id);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut=request;
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE("http://service.melihmucuk.com/ShopArWS.asmx");
androidHttpTransport.debug = true;
try {
androidHttpTransport.call("http://tempuri.org/GetItem", envelope);
SoapObject response = (SoapObject) envelope.getResponse();
for(int i=0;i<response.getPropertyCount();i++){
Object property = response.getProperty(i);
if(property instanceof SoapObject){
SoapObject item = (SoapObject) property;
String seller_id = String.valueOf(item.getProperty("seller_id").toString());
String seller_name = item.getProperty("seller_name").toString();
String item_id = String.valueOf(item.getProperty("item_id").toString());
String price = item.getProperty("price").toString() + " TL";
String desc = item.getProperty("desc").toString();
String title = item.getProperty("title").toString();
String location = item.getProperty("location").toString();
String picture = item.getProperty("picture").toString();
item_details[0] = seller_id;
item_details[1] = seller_name;
item_details[2] = item_id;
item_details[3] = price;
item_details[4] = desc;
item_details[5] = title;
item_details[6] = location;
item_details[7] = picture;
}
}
}
catch (Exception e) {
e.printStackTrace();
}
return item_details;
}
创建
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item);
_title =(TextView) findViewById(R.id.titleTxt);
_sellerName = (TextView) findViewById(R.id.sellerNameTxt);
_location = (TextView) findViewById(R.id.LocationNameTxt);
_price = (TextView) findViewById(R.id.PriceNameTxt);
_desc = (TextView) findViewById(R.id.descNameText);
_picture = (ImageView)findViewById(R.id.itemImage);
button = (Button)findViewById(R.id.BuyButton);
veriler = getIntent().getExtras();
item_id=veriler.getString("item_id");
item_details = new String[8];
new GetItemDetailAS(item_id).execute();
bundle = new Bundle();
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final Dialog dialogConfirm = new Dialog(context);
dialogConfirm.setContentView(R.layout.activity_cart);
dialogConfirm.setTitle("Shopping Cart");
dialogConfirm.setCancelable(false);
dTitle = (TextView) dialogConfirm.findViewById(R.id.cartItemTitle);
dPrice = (TextView) dialogConfirm.findViewById(R.id.cartItemPrice);
dTitle.setText(item_details[5].toString()+":");
dPrice.setText(item_details[3]);
Button completeButton = (Button) dialogConfirm.findViewById(R.id.cartCompleteButton);
Button cancelButton = (Button) dialogConfirm.findViewById(R.id.cartCompleteCancel);
completeButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// complete
dialogConfirm.dismiss();
}
});
cancelButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// cancel
dialogConfirm.dismiss();
}
});
dialogConfirm.show();
}
});
}