android - 如何调用在 android 中返回 arrayList 的 Web 服务?

标签 android

我想在我的 android Activity 中从网络服务中检索一个 arrayList。以下代码不起作用:
如何解决错误?当我尝试这种方式时,res 为空!

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();
      }
    }
  }
}

谢谢,
斯内哈

最佳答案

我无法在 arrayList 上弄清楚。
但我能够调用一个返回对象数组的 WS,然后可以将其转换为 arrayKList,如下所示:

    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 类“Discount”,它实现了 KSOAP2 的 KvmSerializable。

问候,
斯内哈

关于android - 如何调用在 android 中返回 arrayList 的 Web 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5867449/

相关文章:

android - 当我尝试在 Android 11 中读取文件时出现电容器文件系统错误

javascript - 如何在 Cordova 中播放音频文件后打开页面?

java - 如何根据首选项设置刷新MainActivity?

java - canRead() 和 canWrite() 都返回 false

安卓 : Menu items can't be removed in the fragment

android - (Smooth)ScrollToPosition 不能与 RecyclerView 一起正常工作

java - 如何使用android opencv访问CV_HAAR_SCALE_IMAGE

java - Android-使用 eclipse 安装到设备一次安装同一应用程序的两个实例-错误

android - 点击外部或后退按钮后关闭 PopupWindow

android - 如何为一加二创建模拟器?