2011-05-03 42 views
1

我想从我的android活动中的Web服务检索一个arrayList。以下代码不起作用:
如何解决错误?当我尝试这种方式时,res是空的!如何调用返回android中的arrayList的Web服务?

public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    System.out.println("qqqqqqqqqqq"); 
    setContentView(R.layout.second); 
    Button submit = (Button) findViewById(R.id.b01); 
    submit.setOnClickListener(new View.OnClickListener() 
    { 
    public void onClick(View view) 
    { 

     Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show(); 

     EditText txt = (EditText) findViewById(R.id.editText1); 
     String text = txt.toString(); 
     SoapObject request = new SoapObject(Name_Space, Method_Name); 
     PropertyInfo pi1 = new PropertyInfo(); 
     pi1.setName("shopId"); 
     pi1.setValue(text); 
     request.addProperty(pi1); 
     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.setOutputSoapObject(request); 
     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
     try 
     { 
     System.out.println("Hellooooooooooo66666666666666"); 
     androidHttpTransport.call(Soap_Action, envelope); 
     SoapPrimitive res = (SoapPrimitive) envelope.getResponse(); 
     System.out.println("Result SP " + res); 


     ArrayList<Discount> result = (ArrayList<Discount>) envelope.getResponse(); 
     Toast.makeText(getApplicationContext(), "Hi", Toast.LENGTH_SHORT).show(); 
     System.out.println("Result in getResult() : " + res); 

     System.out.println("Traversing ArrayList in forward direction "); 
     if (res != null) 
     { 
      for (Discount ds : result) 
      { 
      discount = ds; 

      } 
     } 
     else 
     { 
      Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show(); 
     } 


     Toast.makeText(getApplicationContext(), envelope.getResponse().toString(), Toast.LENGTH_SHORT) 
      .show(); 
     Toast.makeText(getApplicationContext(), discount.getProductName(), Toast.LENGTH_SHORT).show(); 
     System.out.println("Size 1111 " + result.size()); 


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

感谢,
斯纳

回答

1

我无法找出上的ArrayList。
但我能够调用返回如下然后可以转换为arrayKList对象数组的WS:

try{ 
      System.out.println("Hellooooooooooo66666666666666"); 
      androidHttpTransport.call(Soap_Action, envelope); 
      SoapObject result=(SoapObject)envelope.bodyIn; 
      System.out.println("value of result " + result); 
      System.out.println("Count " + result.getPropertyCount()); 
      System.out.println("Prop 1 " + result.getProperty(0)); 
      System.out.println("A list b4 :; " + arrayList); 
      Discount discount=null; 
      if(result!=null) { 
      for(int i=0;i<result.getPropertyCount();i++){ 
       discount=new Discount((SoapObject)result.getProperty(i)); 
      Toast.makeText(getApplicationContext(),"Hi",Toast.LENGTH_SHORT).show();  

      System.out.println("is result null????????????"+result); 


      arrayList.add(discount); 
      System.out.println("Array ::::::::: " + arrayList); 
      } 
     } 



     for (Discount d : arrayList) { 
      System.out.println(d); 
      System.out.println("Array List Prod Name " + d.getProductName()); 
      System.out.println("Array List Prod id" + d.getProductId()); 
      System.out.println("Array List shop id " + d.getShopId()); 
      System.out.println("Array List discount " + d.getDiscount()); 

     } 



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

你也必须创建一个bean类“折扣”,它实现KSOAP2的KvmSerializable 。

问候,
斯纳

+0

你只问问题,太给答案....伟大... – 2012-11-28 09:26:42

+0

你好,我用你的代码在上面的代码由WS返回ArrayList中你已经使用折扣=新折扣((SoapObject)result.getProperty(I));在这里你正在调用bean,你可以请你发布你的bean,这样我就可以理解我的代码出了什么问题 – varun 2013-04-22 12:26:03