如何使用asynctask检查Web服务的返回值

时间:2014-09-29 09:15:29

标签: android web-services android-asynctask

我所拥有的是一个asynctask,我使用它将数据发送到Web服务,工作正常,这是我的Web服务代码:

public class WebServiceDetails {
    //Namespace of the Webservice - can be found in WSDL
    private static String NAMESPACE = "http://tempuri.org/";
    //Webservice URL - WSDL File location    
    //URL = "http://80.90.161.246:70/erP_Reporting/OrderAndroid.asmx";
    private static String URL = "http://192.168.1.124/alibabanewwebservice/AliBabaWebService.asmx";//Make sure you changed IP address
    //private static String URL = "http://192.168.1.103/webservicejal6ah/OrderAndroid.asmx";//Make sure you changed IP address
    //SOAP Action URI again Namespace + Web method name
    private static String SOAP_ACTION = "http://tempuri.org/InsertOrderDetails";

    public static boolean invokeLoginWS(String DeviceId, String Item_id, String Item_Quantity,String Bounce,String webMethName) 
    {
        boolean loginStatus = false;
        // Create request
        SoapObject request = new SoapObject(NAMESPACE, webMethName);
        // Property which holds input parameters
        PropertyInfo deviceid = new PropertyInfo();
        PropertyInfo itemid = new PropertyInfo();
        PropertyInfo quantity = new PropertyInfo();
        PropertyInfo bounce= new PropertyInfo();


        deviceid.setName("DeviceId");
        deviceid.setValue(DeviceId);
        deviceid.setType(String.class);
        request.addProperty(deviceid);


        itemid.setName("Item_id");
        itemid.setValue(Item_id);
        itemid.setType(String.class);
        request.addProperty(itemid);


        quantity.setName("Item_Quantity");
        quantity.setValue(Item_Quantity);
        quantity.setType(String.class);
        request.addProperty(quantity);


        bounce.setName("Bounce");
        bounce.setValue(Bounce);
        bounce.setType(String.class);
        request.addProperty(bounce);

//        extra.setName("Extra");
//        extra.setValue(Extra);
//        extra.setType(String.class);
//        request.addProperty(extra);


        // Create envelope
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(1);
        envelope.dotNet = true;
        // Set output SOAP object
        envelope.setOutputSoapObject(request);
        // Create HTTP call object
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);


        try {
            // Invoke web service
            androidHttpTransport.call(SOAP_ACTION, envelope);
            // Get the response
//            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
            Object response = (Object) envelope.getResponse();
            // Assign it to  boolean variable variable
            loginStatus = Boolean.parseBoolean(response.toString());

        } catch (Exception e) {
            //Assign Error Status true in static variable 'errored'

            e.printStackTrace();
        } 
        //Return booleam to calling object
        return loginStatus;
    }
}

这是我的asynctask:

private class AsyncCallWS extends AsyncTask<Void, Void, Void> {



    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
         Log.e("Customer_Name","sereen");
        //Make Progress Bar invisible


        //AddToCart.cartlist.clear();
        try{
             //Log.e(" Customer_Id=masterrs.get(m).getCustomerId()", rs.get(m).getCustomerId());
            Toast.makeText(CartList.this, "order has been send ", Toast.LENGTH_LONG).show();
            }
        catch(Exception e){
            e.printStackTrace();
        }

    }


    //Make Progress Bar visible
    protected void onPreExecute() {
       super.onPreExecute();
        pd.setTitle("sending");
        pd.setMessage("waiting...");
        pd.show();
    }



    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub


             loginStatus2 = WebServiceDetails.invokeLoginWS(Device_ID,Item_Id,Quantity,Bounce,"InsertOrderDetails");
             Log.e("Device_ID details",Device_ID+"");
//          try{
             //Log.e(" Customer_Id=masterrs.get(m).getCustomerId()", rs.get(m).getCustomerId());

//          }
//          catch(Exception e){
//              e.printStackTrace();
//          }
//          
//       }
         }


        return null;
    }
    }

好吧,问题是因为你可以看到我的asynctask返回null值,我希望它发送值后返回true(loginStatus2),但问题是当我试图这样做时,它没有工作..任何人都可以帮助我,当成功发布在Web服务上时,我应该改变什么来获得值,而当它失败时,我会做出什么?

1 个答案:

答案 0 :(得分:1)

为AsyncTask尝试此代码。使用结果并检查它是否为空。

如果为null,则没有响应,否则您可以以Asynctask()方法的结果形式获得响应。

private class AsyncCallWS extends AsyncTask<Void, Void, String> {
String response;


protected void onPostExecute(String result) {
    super.onPostExecute(result);
     if(result==null){
        Toast.makeText(CartList.this, "result is null", Toast.LENGTH_LONG).show();
}else{
        Toast.makeText(CartList.this, "Result is: "+ result, Toast.LENGTH_LONG).show();
    }
    try{ //Your code here
         //Log.e(" Customer_Id=masterrs.get(m).getCustomerId()", rs.get(m).getCustomerId()); 
        Toast.makeText(CartList.this, "order has been send ", Toast.LENGTH_LONG).show();
        } 
    catch(Exception e){
        e.printStackTrace();
    } 

} 


//Make Progress Bar visible 
protected void onPreExecute() { 
   super.onPreExecute(); 
    pd.setTitle("sending"); 
    pd.setMessage("waiting..."); 
    pd.show(); 
} 

@Override 
protected Void doInBackground(Void... params) {
    // TODO Auto-generated method stub 


         loginStatus2 = WebServiceDetails.invokeLoginWS(Device_ID,Item_Id,Quantity,Bounce,"InsertOrderDetails");
        response = loginStatus2;
         Log.e("Device_ID details",Device_ID+"");

     } 


    return response; 
} 
}