从 Android 访问 SOAP Web 服务

发布于 2024-12-25 14:29:56 字数 6846 浏览 0 评论 0原文

我正在尝试编写一个 Android 代码来访问基于 SOAP 的 Web 服务。但由于异常,代码在 SoapObject request = new SoapObject(NAMESPACE, METHOD) 处被破坏。

代码如下。有人可以指出哪里出了问题吗?任何有关此问题的线索都将受到赞赏。你们中的任何人都可以使用相同的代码片段吗?

package com.demo;


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.HttpTransportSE;

import com.demo.R.id;

import java.net.*;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.TextView;


public class WebServiceDemoActivity extends Activity {


    private final String NAMESPACE = "http://tempuri.org";
    private final String URL = "http://xxxxx.com/xxx.svc?wsdl";
    private final String SOAPACTION = "http://tempuri.org/IServices/getSearchResults";
    private final String METHOD = "getSearchResults";



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

    }

    public void getPropertySearch(View v){

        TextView response_tv = (TextView)findViewById(id.ResultText);

        final TextView input_zip = (TextView)findViewById(id.ZipCode);
        String zip_value = input_zip.getText().toString();

        final TextView min_price = (TextView)findViewById(id.MinPrice);
        String min_value = input_zip.getText().toString();


        final TextView max_price = (TextView)findViewById(id.MaxPrice);
        String max_value = input_zip.getText().toString();


        if(zip_value == null || zip_value.length() == 0)
        {
            response_tv.setText("Error! Zip Code cannot be Empty");
            return;
        }

        if(min_value == null || min_value.length() == 0)
        {
            response_tv.setText("Error! Min: Price cannot be Empty");
            return;
        }

        if(max_value == null || max_value.length() == 0)
        {
            response_tv.setText("Error! Max: Price cannot be Empty");
            return;
        }


        SoapObject request = new SoapObject(NAMESPACE, METHOD);
        PropertyInfo GetSearchProp = new PropertyInfo();
        GetSearchProp.setName("getSearchResults");
        GetSearchProp.setValue("zip=19119,price>=1000000,price<=1099000");
        GetSearchProp.setType(String.class);
        request.addProperty(GetSearchProp);


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

            System.out.print("Before SOAP call");

            androidHttpTransport.call(SOAPACTION, envelope);
            System.out.print("After SOAP call and getting Response");
            SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
            //response_tv.setText( "Value is "+ response.toString());

            System.out.println(response.toString());
            response_tv.setText( "SUCCESS");
        }catch(Exception e)
        {
            e.printStackTrace();
        }


    }
}

以下是我遇到的异常。

01-06 12:06:09.477: W/System.err(535): android.os.NetworkOnMainThreadException
01-06 12:06:09.477: W/System.err(535):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
01-06 12:06:09.477: W/System.err(535):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
01-06 12:06:09.487: W/System.err(535):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
01-06 12:06:09.487: W/System.err(535):  at java.net.InetAddress.getAllByName(InetAddress.java:220)
01-06 12:06:09.487: W/System.err(535):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
01-06 12:06:09.487: W/System.err(535):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
01-06 12:06:09.487: W/System.err(535):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
01-06 12:06:09.487: W/System.err(535):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
01-06 12:06:09.498: W/System.err(535):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-06 12:06:09.498: W/System.err(535):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
01-06 12:06:09.498: W/System.err(535):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
01-06 12:06:09.498: W/System.err(535):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
01-06 12:06:09.508: W/System.err(535):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
01-06 12:06:09.508: W/System.err(535):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
01-06 12:06:09.508: W/System.err(535):  at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:76)
01-06 12:06:09.508: W/System.err(535):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:152)
01-06 12:06:09.517: W/System.err(535):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95)
01-06 12:06:09.517: W/System.err(535):  at com.demo.WebServiceDemoActivity.getPropertySearch(WebServiceDemoActivity.java:91)
01-06 12:06:09.517: W/System.err(535):  at java.lang.reflect.Method.invokeNative(Native Method)
01-06 12:06:09.517: W/System.err(535):  at java.lang.reflect.Method.invoke(Method.java:511)
01-06 12:06:09.527: W/System.err(535):  at android.view.View$1.onClick(View.java:3039)
01-06 12:06:09.527: W/System.err(535):  at android.view.View.performClick(View.java:3511)
01-06 12:06:09.527: W/System.err(535):  at android.view.View$PerformClick.run(View.java:14105)
01-06 12:06:09.527: W/System.err(535):  at android.os.Handler.handleCallback(Handler.java:605)
01-06 12:06:09.538: W/System.err(535):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-06 12:06:09.538: W/System.err(535):  at android.os.Looper.loop(Looper.java:137)
01-06 12:06:09.538: W/System.err(535):  at android.app.ActivityThread.main(ActivityThread.java:4424)
01-06 12:06:09.547: W/System.err(535):  at java.lang.reflect.Method.invokeNative(Native Method)
01-06 12:06:09.547: W/System.err(535):  at java.lang.reflect.Method.invoke(Method.java:511)
01-06 12:06:09.547: W/System.err(535):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-06 12:06:09.547: W/System.err(535):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-06 12:06:09.547: W/System.err(535):  at dalvik.system.NativeStart.main(Native Method)

I'm trying to write an Android code for accessing SOAP based webservice. But the code is breaking at SoapObject request = new SoapObject(NAMESPACE, METHOD), due to exception.

The code is given below. Can some one point out what went wrong. Any leads on this issue is appreciated. Any code snippets for the same available with any of you?

package com.demo;


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.HttpTransportSE;

import com.demo.R.id;

import java.net.*;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.TextView;


public class WebServiceDemoActivity extends Activity {


    private final String NAMESPACE = "http://tempuri.org";
    private final String URL = "http://xxxxx.com/xxx.svc?wsdl";
    private final String SOAPACTION = "http://tempuri.org/IServices/getSearchResults";
    private final String METHOD = "getSearchResults";



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

    }

    public void getPropertySearch(View v){

        TextView response_tv = (TextView)findViewById(id.ResultText);

        final TextView input_zip = (TextView)findViewById(id.ZipCode);
        String zip_value = input_zip.getText().toString();

        final TextView min_price = (TextView)findViewById(id.MinPrice);
        String min_value = input_zip.getText().toString();


        final TextView max_price = (TextView)findViewById(id.MaxPrice);
        String max_value = input_zip.getText().toString();


        if(zip_value == null || zip_value.length() == 0)
        {
            response_tv.setText("Error! Zip Code cannot be Empty");
            return;
        }

        if(min_value == null || min_value.length() == 0)
        {
            response_tv.setText("Error! Min: Price cannot be Empty");
            return;
        }

        if(max_value == null || max_value.length() == 0)
        {
            response_tv.setText("Error! Max: Price cannot be Empty");
            return;
        }


        SoapObject request = new SoapObject(NAMESPACE, METHOD);
        PropertyInfo GetSearchProp = new PropertyInfo();
        GetSearchProp.setName("getSearchResults");
        GetSearchProp.setValue("zip=19119,price>=1000000,price<=1099000");
        GetSearchProp.setType(String.class);
        request.addProperty(GetSearchProp);


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

            System.out.print("Before SOAP call");

            androidHttpTransport.call(SOAPACTION, envelope);
            System.out.print("After SOAP call and getting Response");
            SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
            //response_tv.setText( "Value is "+ response.toString());

            System.out.println(response.toString());
            response_tv.setText( "SUCCESS");
        }catch(Exception e)
        {
            e.printStackTrace();
        }


    }
}

Below is the exception that I'm getting.

01-06 12:06:09.477: W/System.err(535): android.os.NetworkOnMainThreadException
01-06 12:06:09.477: W/System.err(535):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
01-06 12:06:09.477: W/System.err(535):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
01-06 12:06:09.487: W/System.err(535):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
01-06 12:06:09.487: W/System.err(535):  at java.net.InetAddress.getAllByName(InetAddress.java:220)
01-06 12:06:09.487: W/System.err(535):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
01-06 12:06:09.487: W/System.err(535):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
01-06 12:06:09.487: W/System.err(535):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
01-06 12:06:09.487: W/System.err(535):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
01-06 12:06:09.498: W/System.err(535):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-06 12:06:09.498: W/System.err(535):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
01-06 12:06:09.498: W/System.err(535):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
01-06 12:06:09.498: W/System.err(535):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
01-06 12:06:09.508: W/System.err(535):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
01-06 12:06:09.508: W/System.err(535):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
01-06 12:06:09.508: W/System.err(535):  at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:76)
01-06 12:06:09.508: W/System.err(535):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:152)
01-06 12:06:09.517: W/System.err(535):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95)
01-06 12:06:09.517: W/System.err(535):  at com.demo.WebServiceDemoActivity.getPropertySearch(WebServiceDemoActivity.java:91)
01-06 12:06:09.517: W/System.err(535):  at java.lang.reflect.Method.invokeNative(Native Method)
01-06 12:06:09.517: W/System.err(535):  at java.lang.reflect.Method.invoke(Method.java:511)
01-06 12:06:09.527: W/System.err(535):  at android.view.View$1.onClick(View.java:3039)
01-06 12:06:09.527: W/System.err(535):  at android.view.View.performClick(View.java:3511)
01-06 12:06:09.527: W/System.err(535):  at android.view.View$PerformClick.run(View.java:14105)
01-06 12:06:09.527: W/System.err(535):  at android.os.Handler.handleCallback(Handler.java:605)
01-06 12:06:09.538: W/System.err(535):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-06 12:06:09.538: W/System.err(535):  at android.os.Looper.loop(Looper.java:137)
01-06 12:06:09.538: W/System.err(535):  at android.app.ActivityThread.main(ActivityThread.java:4424)
01-06 12:06:09.547: W/System.err(535):  at java.lang.reflect.Method.invokeNative(Native Method)
01-06 12:06:09.547: W/System.err(535):  at java.lang.reflect.Method.invoke(Method.java:511)
01-06 12:06:09.547: W/System.err(535):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-06 12:06:09.547: W/System.err(535):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-06 12:06:09.547: W/System.err(535):  at dalvik.system.NativeStart.main(Native Method)

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

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

发布评论

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

评论(2

゛时过境迁 2025-01-01 14:29:56

由于从 HoneyComb 版本开始启用 Strictmode,您无法在主线程内执行 HTTP 请求。您必须在主线程中启动辅助线程并调用其中的 Web 服务:

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);       
    //...............
    new Thread(new Runnable()
    {
        public void run()
        {
            androidHttpTransport.call(SOAPACTION, envelope);
        }
    }).start();
}

As Strictmode is enabled Since HoneyComb version, you can't perform HTTP requests inside the main thread. You have to launch a secondary thread inside the main and call the webservice inside it:

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);       
    //...............
    new Thread(new Runnable()
    {
        public void run()
        {
            androidHttpTransport.call(SOAPACTION, envelope);
        }
    }).start();
}
独木成林 2025-01-01 14:29:56

根据我的说法,你应该改变你的肥皂行动

private final String SOAPACTION = "http://tempuri.org/getSearchResults";

,因为

SOAP ACTION=NAMESPACE+METHOD NAME

尝试使用这个可能会帮助你

According to me you should change your soap aCtion

private final String SOAPACTION = "http://tempuri.org/getSearchResults";

its because

SOAP ACTION=NAMESPACE+METHOD NAME

try using this may be it would help u out

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