在 Android 中显示 Soap 响应(数组)

发布于 2025-01-03 10:35:24 字数 6317 浏览 0 评论 0原文

我研究了很多并试图解决它,但我面临着在屏幕上显示响应的问题。

Soap 服务代码是::

POST /ws1.asmx HTTP/1.1
Host: www.xyz.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://xyx.com/webservices/LoadServices"

<?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>
    <LoadServices xmlns="http://xyz.com/webservices/" />
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?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>
    <LoadServicesResponse xmlns="http://xyz.com/webservices/">
      <LoadServicesResult>
        <anyType />
        <anyType />
      </LoadServicesResult>
    </LoadServicesResponse>
  </soap:Body>
</soap:Envelope>

我在 Java 中使用 2 个类。这是它的代码。

首先,我添加 Category.java 代码:

import java.util.Hashtable;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

import android.util.Log;

public class Category implements KvmSerializable
{
    public int CompanyId;               //0
    public int ServiceId;               //1
    public String ServiceMessage;       //2
    public int ServiceCategoryId;       //3
    public boolean IsVisibleForUser;    //4
    public String ServiceName;          //5
    public String Serviceform;          //6
    public char IsActive;               //7
    public String ServiceImage;         //8
    public String ServiceCode;          //9
    public String RequestFulfilledMsg;  //10
    public String ToDoPendingRequest;   //11
    public String CompanyName;          //12
    public String ServiceCategoryName;  //13
    public String ThumbnailImage;       //14    
    public String ServiceDescription;   //15





    public Category()
    {}

     public Category(int coid, int servId, int scid)
     {
         CompanyId = coid;
         ServiceId = servId;
         ServiceCategoryId = scid;

     }


    public Object getProperty(int arg0)
    {
        Log.i("Category", "msg:getProperty");
        switch(arg0)
        {
        case 0:
            return CompanyId;

        case 1:
            return ServiceId;

        case 3:
            return ServiceCategoryId;

        }

        return null;
    }




    public int getPropertyCount()
    {
        Log.i("Category", "msg:getPropertyCount(3)");
        return 3;
    }

    public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info)
    {
        Log.i("Category", "msg:getPropertyInfo");
        switch (index)
        {
        case 0:
            info.type = PropertyInfo.INTEGER_CLASS;
            info.name = "CompanyId";
            break;

        case 1:
            info.type = PropertyInfo.INTEGER_CLASS;
            info.name = "ServiceId";
            break;

        case 3:
            info.type = PropertyInfo.INTEGER_CLASS;
            info.name = "ServiceCategoryId";
            break;


            default:
                break;
        }

    }


    public void setProperty(int index, Object value)
    {
        Log.i("Category", "msg:SetProperty");
        switch (index)
        {

        case 0:
            CompanyId = Integer.parseInt(value.toString());
            break;

        case 1:
            ServiceId = Integer.parseInt(value.toString());
            break;

        case 3:
            ServiceCategoryId = Integer.parseInt(value.toString());
            break;


        default:
            break;
        }

    }

}

现在是根活动的代码,即 WebServiceEx.java

public class WebServiceEx extends Activity
{
    private final String NAMESPACE = "http://xyz.com/webservices/";
    private final String URL = "http://www.abc.com/ws1.asmx";
    private final String SOAP_ACTION = "http://xyz.com/webservices/LoadServices";
    private final String METHOD_NAME = "LoadServices";

    //public Array array = null;
    //String arr[] = null;

    TextView tv;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        tv = new TextView(this);

        LoadSoapService();
        setContentView(tv);
    }


    private void LoadSoapService()
    {
        Log.i("WebService", "msg:soap enter");
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);


        Category C = new Category();
        C.ServiceCategoryId = 1;
        Log.i("WebService", "msg:Category");

        Log.i("WebService", "msg:Property");

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

        Log.i("WebService", "msg:Request");

        envelope.addMapping(NAMESPACE, "Category", new Category().getClass());

        HttpTransportSE transportSE = new HttpTransportSE(URL);

        Log.i("WebService", "msg:try_out");
        try
        {
                                Log.i("WebService", "msg:try+in");
            transportSE.call(SOAP_ACTION, envelope);                            
                                Log.i("WebService", "msg:SoapObject");
            SoapObject response = (SoapObject)envelope.getResponse();               /**error here, wents to exception **/

                                Log.i("WebService", "Response");

                                Log.i("WebSrv", response.toString());


            C.ServiceCategoryId = Integer.parseInt(response.getProperty(3).toString());

            tv.setText(" "+C.ServiceCategoryId);


        }
        catch (Exception e) 
        {
            e.printStackTrace();
            Log.i("WebService", "msg:Exception error");
        }

    }
}

我已经添加了问题发生位置的注释,此外,如果我遗漏了某些内容,请告诉我,因为一切都在您面前。

我已将 ksoap2 添加到外部 Jar 并添加了 Internet 权限,我遇到的问题是黑屏。

希望得到一些想法,

谢谢, 哈普雷特。

I studied a lot and tried to solve it but I am facing the problem to show the response on screen.

Soap Service Code is::

POST /ws1.asmx HTTP/1.1
Host: www.xyz.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://xyx.com/webservices/LoadServices"

<?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>
    <LoadServices xmlns="http://xyz.com/webservices/" />
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?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>
    <LoadServicesResponse xmlns="http://xyz.com/webservices/">
      <LoadServicesResult>
        <anyType />
        <anyType />
      </LoadServicesResult>
    </LoadServicesResponse>
  </soap:Body>
</soap:Envelope>

I am using 2 classes in Java. And here are the codes of it.

Firstly I am adding Category.java Code:

import java.util.Hashtable;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

import android.util.Log;

public class Category implements KvmSerializable
{
    public int CompanyId;               //0
    public int ServiceId;               //1
    public String ServiceMessage;       //2
    public int ServiceCategoryId;       //3
    public boolean IsVisibleForUser;    //4
    public String ServiceName;          //5
    public String Serviceform;          //6
    public char IsActive;               //7
    public String ServiceImage;         //8
    public String ServiceCode;          //9
    public String RequestFulfilledMsg;  //10
    public String ToDoPendingRequest;   //11
    public String CompanyName;          //12
    public String ServiceCategoryName;  //13
    public String ThumbnailImage;       //14    
    public String ServiceDescription;   //15





    public Category()
    {}

     public Category(int coid, int servId, int scid)
     {
         CompanyId = coid;
         ServiceId = servId;
         ServiceCategoryId = scid;

     }


    public Object getProperty(int arg0)
    {
        Log.i("Category", "msg:getProperty");
        switch(arg0)
        {
        case 0:
            return CompanyId;

        case 1:
            return ServiceId;

        case 3:
            return ServiceCategoryId;

        }

        return null;
    }




    public int getPropertyCount()
    {
        Log.i("Category", "msg:getPropertyCount(3)");
        return 3;
    }

    public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info)
    {
        Log.i("Category", "msg:getPropertyInfo");
        switch (index)
        {
        case 0:
            info.type = PropertyInfo.INTEGER_CLASS;
            info.name = "CompanyId";
            break;

        case 1:
            info.type = PropertyInfo.INTEGER_CLASS;
            info.name = "ServiceId";
            break;

        case 3:
            info.type = PropertyInfo.INTEGER_CLASS;
            info.name = "ServiceCategoryId";
            break;


            default:
                break;
        }

    }


    public void setProperty(int index, Object value)
    {
        Log.i("Category", "msg:SetProperty");
        switch (index)
        {

        case 0:
            CompanyId = Integer.parseInt(value.toString());
            break;

        case 1:
            ServiceId = Integer.parseInt(value.toString());
            break;

        case 3:
            ServiceCategoryId = Integer.parseInt(value.toString());
            break;


        default:
            break;
        }

    }

}

And now the code of root activity, i.e. WebServiceEx.java

public class WebServiceEx extends Activity
{
    private final String NAMESPACE = "http://xyz.com/webservices/";
    private final String URL = "http://www.abc.com/ws1.asmx";
    private final String SOAP_ACTION = "http://xyz.com/webservices/LoadServices";
    private final String METHOD_NAME = "LoadServices";

    //public Array array = null;
    //String arr[] = null;

    TextView tv;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        tv = new TextView(this);

        LoadSoapService();
        setContentView(tv);
    }


    private void LoadSoapService()
    {
        Log.i("WebService", "msg:soap enter");
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);


        Category C = new Category();
        C.ServiceCategoryId = 1;
        Log.i("WebService", "msg:Category");

        Log.i("WebService", "msg:Property");

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

        Log.i("WebService", "msg:Request");

        envelope.addMapping(NAMESPACE, "Category", new Category().getClass());

        HttpTransportSE transportSE = new HttpTransportSE(URL);

        Log.i("WebService", "msg:try_out");
        try
        {
                                Log.i("WebService", "msg:try+in");
            transportSE.call(SOAP_ACTION, envelope);                            
                                Log.i("WebService", "msg:SoapObject");
            SoapObject response = (SoapObject)envelope.getResponse();               /**error here, wents to exception **/

                                Log.i("WebService", "Response");

                                Log.i("WebSrv", response.toString());


            C.ServiceCategoryId = Integer.parseInt(response.getProperty(3).toString());

            tv.setText(" "+C.ServiceCategoryId);


        }
        catch (Exception e) 
        {
            e.printStackTrace();
            Log.i("WebService", "msg:Exception error");
        }

    }
}

I had add the comment from where the problem occurs, Moreoever if I am missing something do tell me, As everything is in front of you.

I have added ksoap2 to external Jar and has added Internet Permissions, All I have problem is a blank screen.

Hope to get some idea,

Thanks,
Harpreet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软的没边 2025-01-10 10:35:24

如果我是你,我会

private void LoadSoapService()

按如下方式使用此方法:

private String LoadSoapService()

返回 Integer.parseInt(response.getProperty(3).toString());
并将 TextViews 文本设置为

tv.setText(LoadSoapService);

If I were you i would make

private void LoadSoapService()

this metod as follow:

private String LoadSoapService()

which returns Integer.parseInt(response.getProperty(3).toString());
and set TextViews text to

tv.setText(LoadSoapService);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文