简体   繁体   English

Java Android:使用SOAP从Web服务获取Xml文件

[英]Java Android: Getting Xml File from Webservice with SOAP

I have a C# Webservice and an Eclipse Android Application project. 我有一个C#Web服务和一个Eclipse Android应用程序项目。 I am trying to get a xml file from the webservice with KSOAP2 version 2.3. 我正在尝试使用KSOAP2 2.3版从Web服务获取xml文件。 The webservice works and gives me back the current xml document and it also seems like i get a connection to the webservice with the android application, but i always get an exception saying that the webservice does not recognize the value of the HTTP Header "SOAPAction". 该Web服务正常工作,并给我返回了当前的xml文档,似乎我也已使用android应用程序连接到该Web服务,但我总是收到一个异常,说该Web服务无法识别HTTP标头“ SOAPAction”的值。

Webservice: 网络服务:

    namespace WebService3
    {
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // [System.Web.Script.Services.ScriptService]


    class DBConnect
    {
        private MySqlConnection connection;
        private string server;
        private string database;
        private string uid;
        private string password;
    }

    private void Initialize()
    {
            server = "localhost";
            database = "eagles_db";
            uid = "root";
            password = "";
            string connectionString;
            connectionString = "SERVER=" + server + ";" + "DATABASE=" +
            database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

            connection = new MySqlConnection(connectionString);
    }

public XmlDocument abmeldungen()
        {
            XmlDocument doc = new XmlDocument();
            XmlNode root;
            XmlNode tempHeader;
            XmlNode tempInhalt;

            List<int> lstIDs = new List<int>();
            List<string> lstNamen = new List<string>();
            string ausgabe = string.Empty;



            string conStr = ConfigurationManager.ConnectionStrings["dbstring"].ConnectionString;

            string befehl = "Select abmeldung.cf_created, abmeldung.datum, abmeldung.grund, web_users.name From abmeldung Inner Join web_users On abmeldung.cf_user_id = web_users.id Order By abmeldung.cf_created Desc";



            MySql.Data.MySqlClient.MySqlCommand cmd = new MySqlCommand(befehl, connection);



            connection.Open();

            MySql.Data.MySqlClient.MySqlDataReader reader = cmd.ExecuteReader();



            root = doc.CreateElement("Abmeldungen");
            doc.AppendChild(root);


            tempHeader = doc.CreateElement("Abmeldungen"); //doc.CreateElement(reader["reportTitle"].ToString());

            root.AppendChild(tempHeader);

            while (reader.Read())
            {
                tempInhalt = doc.CreateElement("Name");
                tempHeader.AppendChild(tempInhalt).InnerText = reader["name"].ToString();

                tempInhalt = doc.CreateElement("Grund");
                tempHeader.AppendChild(tempInhalt).InnerText = reader["grund"].ToString();

                tempInhalt = doc.CreateElement("datum");
                tempHeader.AppendChild(tempInhalt).InnerText = reader["datum"].ToString();

                tempInhalt = doc.CreateElement("erstellt");
                tempHeader.AppendChild(tempInhalt).InnerText = reader["cf_created"].ToString();
            }

            reader.Close();

            connection.Close();

            return doc;

        }

        [WebMethod]
        public XmlDocument abmeldung()
        {
            DBConnect con1 = new DBConnect();
            return con1.abmeldungen();
        }

everything works in firefox and i get the correct Xml document 一切都在firefox中工作,我得到正确的Xml文档

Application: 应用:

package de.maturaprojekt.sema;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;


import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;


public class Abmeldungen extends Activity {

    public static String NAMESPACE = "http://tempuri.org/";
    public static String SOAP_ACTION = "http://10.0.2.2:28266/Service1.asmx/abmeldung";
    public static String METHOD_NAME = "abmeldung";

    public static String URL = "http://10.0.2.2:28266/Service1.asmx";

    public static String response;

    public static String abmeldungen1()
    {
        //SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        return "Error";
    }

    public static String abmeldungen()
    {


        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);


        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.setOutputSoapObject(request);

        try
        {
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            androidHttpTransport.call(SOAP_ACTION, envelope);

            // SoapResult
            SoapObject result = (SoapObject)envelope.getResponse(); //body.In


            if (result != null)
            {
                response=result.toString(); //getProperty(0)

                if(response.equals("false"))
                {
                    return "Error";
                    //error, 
                }

                else
                {
                    return "IT WORKS";
                    // everything works correctly
                }

            }

            else
            {
                return "ERROR2";
                // no answer from webservice
            }
        }

        catch (Exception exc)
        {
            Log.i("tom",exc.toString());
            return "ERROR3";
            // andere Probleme
        }



    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_abmeldungen);
    }

    public void btn_abfragen(View view)
    {
        //Toast.makeText(this, "abgerufen", Toast.LENGTH_SHORT).show();
        String return22 = abmeldungen();

        Toast.makeText(this,return22, Toast.LENGTH_LONG).show();

    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.abmeldungen, menu);
        return true;
    }
}

I used 10.10.2.2 as localhost because on the emulated android device the normal "localhost" would be localhost of the emulated device and 10.10.2.2 is the right localhost. 我使用10.10.2.2作为本地主机,因为在模拟的android设备上,正常的“本地主机”将是模拟设备的本地主机,而10.10.2.2是正确的本地主机。

I hope my question isn't too confusing, i would appreciate it very much if anybody could help me. 我希望我的问题不要太混乱,如果有人可以帮助我,我将不胜感激。 Thank you 谢谢

public static String SOAP_ACTION = "http://10.0.2.2:28266/Service1.asmx/abmeldung";

I think your soap action should be something like, 我认为您的肥皂动作应该是这样的,

"http://tempuri.org/Service1/abmeldung"

You need to check this in your wsdl. 您需要在wsdl中进行检查。

Open your wsdl in browser using, 使用浏览器在浏览器中打开wsdl,

http://10.0.2.2:28266/Service1.asmx

Search wsdl:binding tag in wsdl. 在wsdl中搜索wsdl:binding标记。

Within binding tag search for operation ambeldung 在绑定标签中搜索操作ambeldung

You can find your SOAP Action defined as, 您可以找到定义为的SOAP操作,

<wsdl:binding name="BasicHttpBinding_Service1" type="tns:example">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="ambeldung">
<soap:operation soapAction="http://tempuri.org/examplesoapaction/example" style="document"/>
......
......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM