4

アプリケーションで.NETWebサービスを使用しようとしています。このサービスでは、サービスがオブジェクトの配列を応答として返します。

これは、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 "";
}

asynctaskを使用する

プライベートクラス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 に答える