KSOAP ANDROID实施

时间:2012-06-22 11:00:30

标签: android ksoap

您好我有一个与Android K-SOAP相关的问题,我第一次使用K-SOAP Android

this is the service link

在这里你可以看到请求,那里也提到了响应, 这是我的代码来点击服务并获得响应         包com.ksoap.net;

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 org.ksoap2.transport.HttpTransportSE;

   import android.app.Activity;
   import android.os.Bundle;
   import android.util.Log;
   import android.widget.TextView;

public class AndroidWebService extends Activity {

        private final String NAMESPACE = "http://webservices.iyogi.net/";
                                                                                          private                                                                                 final                                                                        String                                                             URL                                                  =                                          "http://sdservices.iyogi.net               /iyogi           /webservicesnonrestv5/toolbarwebservices.asmx";
        private final String SOAP_ACTION = "http://webservices.iyogi.net/GetSubscriptionByInputObject";
        private final String METHOD_NAME = "GetSubscriptionByInputObject";
        String request = "<?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://www.w3.org/2003/05/soap-envelope\">"
                + "<soap:Body>"
                + "<GetSubscriptionByInputObject xmlns=\"http://webservices.iyogi.net\">"
                + "<inpAccessCode>" + "<AccessCode>”2466276627755434”</AccessCode>"
                + "</inpAccessCode>" + "</GetSubscriptionByInputObject>"
                + "</soap:Body>" + "</soap:Envelope>";

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            // SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

            String weight = "3700";
            String fromUnit = "Grams";
            String toUnit = "Kilograms";


            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
                    URL);

            try {
                androidHttpTransport.call(SOAP_ACTION, envelope);
                SoapObject response = (SoapObject) envelope.getResponse();
                int result = Integer.parseInt(response.getProperty(0).toString());
                System.out.println("result  is " + Integer.toString(result));
                System.out.println("res is -->>  "
                        + response.getProperty(0).toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
}`

请帮助我点击并获得此服务的正确回复 任何帮助都非常感谢

2 个答案:

答案 0 :(得分:0)

试试这个 `

 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 org.ksoap2.transport.HttpTransportSE;

       import android.app.Activity;
       import android.os.Bundle;
       import android.util.Log;
       import android.widget.TextView;

    public class AndroidWebService extends Activity {

            private final String NAMESPACE = "http://webservices.iyogi.net/";
                                                                                              private                                                                                 final                                                                        String                                                             URL                                                  =                                          "http://sdservices.iyogi.net               /iyogi           /webservicesnonrestv5/toolbarwebservices.asmx";
            private final String SOAP_ACTION = "http://webservices.iyogi.net/GetSubscriptionByInputObject";
            private final String METHOD_NAME = "GetSubscriptionByInputObject";
    private final String URL = "http://sdservices.iyogi.net/iyogi/webservicesnonrestv5/toolbarwebservices.asmx";

HttpTransportSE httpTransport = null;

        SoapObject request = null;
    request = new SoapObject(NAMESPACE, METHOD_NAME);
            PropertyInfo pi = new PropertyInfo();       
            pi.setName("AccessCode");
            pi.setValue("2466276627755434");
            pi.setType(PropertyInfo.STRING_CLASS);
            request.addProperty(pi);
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.dotNet = true;

            envelope.setOutputSoapObject(request);

            httpTransport = new HttpTransportSE(URL);
            httpTransport.debug = true;


            try {
                httpTransport.call(SOAP_ACTION, envelope);

    SoapObject response = (SoapObject) envelope.getResponse();
                    int result = Integer.parseInt(response.getProperty(0).toString());
                    System.out.println("result  is " + Integer.toString(result));
                    System.out.println("res is -->>  "
                            + response.getProperty(0).toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }

`

答案 1 :(得分:0)

  final String URL = "url";
              u = new java.net.URL(URL);

             URLConnection uc = u.openConnection();
             connection = (HttpURLConnection) uc;

             connection.setDoOutput(true);
             connection.setDoInput(true);
             connection.setRequestProperty("SOAPAction", SOAP_ACTION);
             connection.setRequestMethod("POST");
             connection.setRequestProperty("Content-type", "text/xml; charset=utf-8");

             String xmldata ="xml request";


             //System.out.println(xmldata);
             OutputStream out = connection.getOutputStream();

             Writer wout = new OutputStreamWriter(out);

              wout.write(xmldata);

                wout.flush();

                wout.close();
                InputStream is = connection.getInputStream();
                MakeHttpRequest.actiovationParser(is,AndroidWebService.this);

它工作正常 欢呼快乐的编码