与ksoap2 LIB SOAP的Web服务 [英] SOAP web service with ksoap2 lib

查看:189
本文介绍了与ksoap2 LIB SOAP的Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用.NET SOAP Web服务与ksoap2库。从 http://www.vimeo.com/9633556 的例子展示了如何做到这一点是正确的。下面从例子中,code。一切工作768,16 OK!但我得到强制关闭错误!

I'm trying to use .net SOAP web service with ksoap2 lib. Example from http://www.vimeo.com/9633556 shows how to do it correct. Below the code from that example. everything shoud work ok ! but i get force Close error !

在这里我的code:

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

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

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

    TextView TV ;
    TV=(TextView)findViewById(R.id.textView1);
    TV.setText("Hi");

    String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
    String METHOD_NAME = "CelsiusToFahrenheit";
    String NAMESPACE = "http://tempuri.org/";
    String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";

    System.setProperty("http.keepAlive", "false");

    SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
    Request.addProperty("Celsius", "32");

    SoapSerializationEnvelope soapEnvelope = new                       SoapSerializationEnvelope(SoapEnvelope.VER11);
    soapEnvelope.dotNet = true;
    soapEnvelope.setOutputSoapObject(Request);

    HttpTransportSE httpTransport = new HttpTransportSE(URL);
    try
    {
        httpTransport.call(SOAP_ACTION, soapEnvelope);
        SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
        TV.setText(resultString.toString());
    }
    catch (Exception e) {
        e.printStackTrace();
    }

     }
    }

请帮忙meeeeee

Please HelP meeeeee

推荐答案

您code是正确的,但只能在Android的工作。2.如果使用V4 ICS / JB你一定要使用AsyncTask的。

Your code is correct but only can work in Android 2. If you use v4 ICS/JB you must be use AsyncTask.

package com.example.wscall3;

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

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {
    private String TAG ="Vik";

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

        AsyncCallWS task = new AsyncCallWS();
        task.execute(); 
    }

    private class AsyncCallWS extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            Log.i(TAG, "doInBackground");
            calculate();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            Log.i(TAG, "onPostExecute");
        }

        @Override
        protected void onPreExecute() {
            Log.i(TAG, "onPreExecute");
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            Log.i(TAG, "onProgressUpdate");
        }

    }

    public void calculate() 
    {
        String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
        String METHOD_NAME = "CelsiusToFahrenheit";
        String NAMESPACE = "http://tempuri.org/";
        String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";

        try { 
            SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
            Request.addProperty("Celsius", "32");

            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            soapEnvelope.dotNet = true;
            soapEnvelope.setOutputSoapObject(Request);

            HttpTransportSE transport= new HttpTransportSE(URL);

            transport.call(SOAP_ACTION, soapEnvelope);
            SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();

            Log.i(TAG, "Result Celsius: " + resultString);
        }
        catch(Exception ex) {
            Log.e(TAG, "Error: " + ex.getMessage());
        }

        SOAP_ACTION = "http://tempuri.org/FahrenheitToCelsius";
        METHOD_NAME = "FahrenheitToCelsius";
        try { 
            SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
            Request.addProperty("Fahrenheit", "100");

            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            soapEnvelope.dotNet = true;
            soapEnvelope.setOutputSoapObject(Request);

            HttpTransportSE transport= new HttpTransportSE(URL);

            transport.call(SOAP_ACTION, soapEnvelope);
            SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();

            Log.i(TAG, "Result Fahrenheit: " + resultString);
        }
        catch(Exception ex) {
            Log.e(TAG, "Error: " + ex.getMessage());
        }
    }

}

我的Andr​​oidManifest.xml是:

My AndroidManifest.xml is:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.wscall3"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="16" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.wscall3.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这篇关于与ksoap2 LIB SOAP的Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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