4

我试图在我的应用程序中使用 .NET Web 服务,该服务返回一组对象作为响应。

这是来自网络服务的响应格式。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetPickersResponse xmlns="http://tempuri.org/">
  <GetPickersResult>
    <Picker>
      <Id>int</Id>
      <StartTime>dateTime</StartTime>
      <EndTime>dateTime</EndTime>
      <PickerCount>int</PickerCount>
    </Picker>
    <Picker>
      <Id>int</Id>
      <StartTime>dateTime</StartTime>
      <EndTime>dateTime</EndTime>
      <PickerCount>int</PickerCount>
    </Picker>
  </GetPickersResult>
</GetPickersResponse>
</soap:Body>
</soap:Envelope>

这是我从 Web 服务获取响应的 Java 代码。

SoapObject request = new SoapObject(NAMESPACE, METHOD_GET_CONTROL);
SoapSerializationEnvelope envelope = 
        new SoapSerializationEnvelope(SoapEnvelope.VER11); 
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);


HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

 try {
             androidHttpTransport.call(SOAP_ACTION_GET_CONTROL, envelope);
            ..........=envelope.getResponse(); //To get the data. }

我的问题是,我应该用什么替换源代码中的“........”来接收对象数组作为来自服务的响应?我需要接收多个对象,然后使用它们各自的数据成员。

请帮忙。我是 Web 服务和 Ksoap 的新手。

4

4 回答 4

6

要获得多重响应,只需在代码中添加几行而不是..........=envelope.getResponse();

SoapObject obj1 = (SoapObject) envelope.getResponse();

SoapObject obj2 =(SoapObject) obj1.getProperty(0);


for(int i=0; i<obj2.getPropertyCount(); i++)
{
   SoapObject obj3 =(SoapObject) obj2.getProperty(i);
   int id= Integer.parseInt(obj3.getProperty(0).toString());
   String start_date = obj3.getProperty(1).toString();
   String end_date = obj3.getProperty(2).toString();
   int id= Integer.parseInt(obj3.getProperty(3).toString());
}

如果你还有什么问题可以给我写信。

于 2012-06-16T11:33:41.597 回答
4

我总是将 Vector 作为响应类型,所以我的解决方案是:

        HttpTransportSE conn = new HttpTransportSE(url);
        try{
            conn.call(soapAction, envelope); //send request
            Vector<SoapObject> result= (Vector<SoapObject>)envelope.getResponse();

            int length = result.size();
            for(int i=0;i<length; ++i){
                SoapObject so = result.get(i);
                Log.d(TAG,so.getPropertyAsString(3));
            }
        } catch(Exception e){
            e.printStackTrace();
        }
于 2013-03-13T11:52:31.807 回答
0
package com.service;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class KSOAP extends Activity 
{
    ListView list;
    ArrayList<String> arraylist = new ArrayList<String>();
    ArrayAdapter<String>  arrayadapter;

    Button btn;
    TextView tv,tv2,tv3; 
    EditText no1,no2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

tv=(TextView)findViewById(R.id.textView1);
tv2=(TextView)findViewById(R.id.textView2);
//tv3=(TextView)findViewById(R.id.textView3);

list=(ListView)findViewById(R.id.listView1);

no1=(EditText)findViewById(R.id.editText1);
no2=(EditText)findViewById(R.id.editText2);

btn =(Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub


        String NAMESPACE = "http://tempuri.org/" ;    
        //"http://vladozver.org/";
        String METHOD_NAME = "GetSumOfTwoIntsList";//"GetStringList"; //
        String SOAP_ACTION = "http://tempuri.org/GetSumOfTwoIntsList";  
        //"http://vladozver.org/GetSumOfTwoInts";
        String URL = "http://192.168.0.203/recharge_service/service.asmx";
        //"http://192.168.0.203/Recharge_Service/Service.asmx";


        SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);

        PropertyInfo pi = new PropertyInfo();
        pi.setName("Operand1");
        pi.setValue(Integer.parseInt(no1.getText().toString()));
        pi.setType(int.class);
        Request.addProperty(pi);

        PropertyInfo pi2 = new PropertyInfo();
        pi2.setName("Operand2");
        pi2.setValue(Integer.parseInt(no2.getText().toString()));
        pi2.setType(int.class);
        Request.addProperty(pi2);


        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        try
        {
        AndroidHttpTransport transp = new AndroidHttpTransport(URL);
        transp.call(SOAP_ACTION, envelope);

        SoapObject obj1 = (SoapObject) envelope.bodyIn;

        SoapObject obj2 =(SoapObject) obj1.getProperty(0);


        for (int i = 0; i< obj2.getPropertyCount(); i++) 
            { 
                 int id1 = Integer.parseInt(obj2.getProperty(0).toString());
              // String id1=obj2.getProperty(0).toString();

               if(id1 != 0)
               { 
                 arraylist.add(""+ id1);    

               }
                /* tv3.setText(id3);*/
            }


        }
        catch(Exception e)
        { tv.setText(e.toString());
        e.printStackTrace();
        }

        //tv.setText(""+ count);



               list.setAdapter(arrayadapter);
    }
});

        arrayadapter = new ArrayAdapter<String>( this, 
        android.R.layout.simple_list_item_1, arraylist );
       list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

        }
    });

}


}
于 2013-04-03T07:04:50.723 回答
0

这是方法

String NAMESPACE = "你的命名空间" ;

//"http://vladozver.org/";

String METHOD_NAME = "get";//"GetStringList"; //

String SOAP_ACTION = "Your SoapAction";

//"http://vladozver.org/GetSumOfTwoInts";

String URL = "Your Soap Url";

public void involk(String na,String WebMethodName) throws IllegalStateException {

    SoapObject request=new SoapObject(NAMESPACE,WebMethodName);

    PropertyInfo sayHellopi=new PropertyInfo();

    sayHellopi.setName("get");
    sayHellopi.setValue(na);
    sayHellopi.setType(String.class);

    request.addProperty(sayHellopi);

    SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);

    AndroidHttpTransport transp = new AndroidHttpTransport(URL);

    try {
        transp.call(SOAP_ACTION,envelope);
        //ArrayList primitive= (ArrayList) envelope.bodyIn;
        SoapObject primitive= (SoapObject) envelope.bodyIn;
        // SoapObject obj2=(SoapObject)obj1.getProperty(0);
        // KvmSerializable ks=(KvmSerializable)envelope.bodyIn;
        // System.out.println("Values.............."+envelope.bodyIn);
               /* for(int i=0;i<obj2.getPropertyCount();i++){
                  String  id1=obj2.getProperty(0).toString();

                    if(""!=id1 ){

                        System.out.println("Count " + obj2.getPropertyCount());
                        arraylist.add(""+id1);
                        System.out.println("Array ::::::::::::: " + arraylist);

                    }
                }*/
                  //resText= envelope.bodyIn;

                   //resText = new ArrayList<Objects>();
           //for(int i=0;i<resText.equals(resText);i++){

          //}
                 System.out.println("List+++++++++++++++++");
               /* //System.out.println("value of result " + primitive);
                System.out.println("Count " + primitive.getPropertyCount());
                //System.out.println("Prop 1 " + primitive.getProperty(0));
                //System.out.println("A list b4 :; " + arrayList);*/

    /* for(int i=0;i<obj1.getPropertyCount();i++){
             //discount=new Discount((SoapObject)primitive.getProperty(i));
            //Toast.makeText(getApplicationContext(),"Hi",Toast.LENGTH_SHORT).show();

            //System.out.println("is result null????????????"+result);
            arraylist.add(obj1.getPropertyCount());
            System.out.println("Array ::::::::: " + arraylist);
        }*/
       // arraylist=new ArrayList();
        //resText=new String[primitive.getPropertyCount()];

        for(int i=0;i<primitive.getPropertyCount();i++){

            arraylist.add((String) primitive.getProperty(i));
            //arraylist.add(primitive.getPropertyCount());
            System.out.println("ForLoop--------------"+primitive.getProperty(i));


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

        }
        // ListIterator it = arraylist.listIterator();

        //while(it.hasNext())
        // {
        // System.out.println("arrayListValue--------------"+it.next());
        //}
        // System.out.println("List+++++++++++++++++"+resText.length);

        // list.setAdapter(arrayadapter);

    }catch (Exception e){
        System.out.println("Error Value" + e);
        //resText="Error Occur";
        e.printStackTrace();
    }
    //list.setAdapter(arrayadapter);
  //return "";
}

使用异步任务

私有类 AsyncCall 扩展 AsyncTask {

    protected void onPreExecute(){

        super.onPreExecute();

        // pg.setVisibility(View.VISIBLE);
        dialog=new ProgressDialog(MainActivity.this);
        dialog.setIndeterminate(false);
        dialog.setMessage("Loding...");
        dialog.setCancelable(false);
        dialog.show();
    }
    protected Void doInBackground(Void... parms){
      involk(editText,"get");
       // return null;
       // return null;
        return null;
    }

    protected void onPostExecute(Void result){
        super.onPreExecute();
        // tv.setText(displayText);
        if(arraylist.size()!=0){
            dialog.dismiss();
        arrayadapter = new ArrayAdapter<String>( MainActivity.this,
                android.R.layout.simple_list_item_activated_1, arraylist );
            list.setAdapter(arrayadapter);
        }else{
            dialog.dismiss();
            getError();
        }
        // pg.setVisibility(View.INVISIBLE);
    }
}

按钮点击事件

btn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

            if(no1.getText().length()!=0&&no1.getText().length()!=0){

                editText=no1.getText().toString();
                AsyncCall task=new AsyncCall();
                task.execute();
            }
        }
    });

我希望这会帮助你的朋友:)

于 2014-05-08T14:13:08.010 回答