如何从Android的使用KSOAP2调用.NET web服务? [英] How to call a .NET Webservice from Android using KSOAP2?

查看:259
本文介绍了如何从Android的使用KSOAP2调用.NET web服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,同时调用web服务。我有一个服务器上的.NET Web服务,而我使用KSOAP2(ksoap2-J2SE的全2.1.2)的Andr​​oid系统。当运行程序我得到一个运行时异常,如org.ksoap2.serialization.SoapPrimitive。我该怎么办?

下面是我的code。

 包projects.ksoap2sample;

进口org.ksoap2.SoapEnvelope;
进口org.ksoap2.serialization.SoapObject;
进口org.ksoap2.serialization.SoapSerializationEnvelope;
进口org.ksoap2.transport.HttpTransportSE;

进口android.app *。
进口android.os *。
进口android.widget.TextView;

公共类ksoap2sample延伸活动{
    / **第一次创建活动时调用。 * /
    私有静态最后弦乐SOAP_ACTION =htt​​p://tempuri.org/HelloWorld;
    私有静态最后弦乐METHOD_NAME =HelloWorld的;
    私有静态最后弦乐NAMESPACE =htt​​p://tempuri.org/;
    私有静态最后字符串的URL =htt​​p://192.168.1.19/TestWeb/WebService.asmx;
    TextView的电视;
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        电视=(TextView中)findViewById(R.id.text1);

        尝试 {
            SoapObject请求=新SoapObject(命名空间METHOD_NAME);

            //request.addProperty("prop1,myprop);

            SoapSerializationEnvelope包=新SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = TRUE;
            envelope.setOutputSoapObject(要求);

            HttpTransportSE androidHttpTransport =新HttpTransportSE(URL);

            androidHttpTransport.call(SOAP_ACTION,包);

            对象result =(对象)envelope.getResponse();

            的String []的结果=(字符串[])的结果;
            tv.setText(+结果[0]);
        }
        赶上(例外五){
            tv.setText(e.getMessage());
        }
    }
}
 

解决方案

这很简单。你所得到的结果转换为对象这是一个基本的。

您code:

 对象result =(对象)envelope.getResponse();
 

正确的code:

  SoapObject结果=(SoapObject)envelope.getResponse();

//获取数据。
串resultData = result.getProperty(0)的ToString();
// 0是数据的第一个对象。
 

我觉得这绝对应该工作。

I have a problem while calling a webservice. I have a .NET web service on the server, and I am using KSOAP2 (ksoap2-j2se-full-2.1.2) in Android. While running the program I got an runtime exception like "org.ksoap2.serialization.SoapPrimitive". What should I do?

Here is my code.

package projects.ksoap2sample;

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

import android.app.*;
import android.os.*;
import android.widget.TextView;

public class ksoap2sample extends Activity {
    /** Called when the activity is first created. */
    private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
    private static final String METHOD_NAME = "HelloWorld";
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://192.168.1.19/TestWeb/WebService.asmx";
    TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv=(TextView)findViewById(R.id.text1);

        try {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

            //request.addProperty("prop1", "myprop");

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

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            androidHttpTransport.call(SOAP_ACTION, envelope);

            Object result = (Object)envelope.getResponse();

            String[] results = (String[])  result;
            tv.setText( ""+results[0]);
        }
        catch (Exception e) {
            tv.setText(e.getMessage());
        }
    }
}

解决方案

It's very simple. You are getting the result into an Object which is a primitive one.

Your code:

Object result = (Object)envelope.getResponse();

Correct code:

SoapObject result=(SoapObject)envelope.getResponse();

//To get the data.
String resultData=result.getProperty(0).toString();
// 0 is the first object of data.

I think this should definitely work.

这篇关于如何从Android的使用KSOAP2调用.NET web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆