gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:41:45 25 4
gpt4 key购买 nike

我想在我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com