2

我正在尝试将 .net SOAP Web 服务与 ksoap2 lib 一起使用。http://www.vimeo.com/9633556中的示例显示了如何正确执行此操作。在该示例的代码下方。一切都应该正常!但我得到强制关闭错误!

这是我的代码:

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

4

3 回答 3

2

您的代码是正确的,但只能在 Android 2 中运行。如果您使用 v4 ICS/JB,则必须使用 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());
        }
    }

}

我的 AndroidManifest.xml 是:

<?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>
于 2012-12-11T21:44:41.443 回答
0

从 developer.android.com 连接到网络

网络操作可能涉及不可预知的延迟。为防止这导致糟糕的用户体验,请始终在与 UI 不同的线程上执行网络操作。AsyncTask 类提供了一种从 UI 线程触发新任务的最简单方法。

如果您不在httpTransport.call(SOAP_ACTION, soapEnvelope);单独的线程上执行网络操作(如 ),您将获得 Android 操作系统的强制关闭对话框。

您可以使用AsyncTaskServiceIntentService来执行此操作。编写另一个类来扩展这些类中的一个以进行 Web 服务调用将解决您的问题。

于 2012-12-12T15:20:10.640 回答
0
public  String SOAP_ACTION;
public  String METHOD_NAME; 
public  final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
public  final String SOAP_ADDRESS =    "http://www.w3schools.com/webservices/tempconvert.asmx";
public  final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";

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

    input = (EditText) findViewById(R.id.editText1);
    result = (EditText) findViewById(R.id.editText2);

    cel= (RadioButton) findViewById(R.id.radioButton1);
    fah= (RadioButton) findViewById(R.id.radioButton2);

    rGroup = (RadioGroup) findViewById(R.id.rgTemp);

    open = (Button) findViewById(R.id.button1);

    open.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Intent openScreen = new Intent("com.loginworks.demo.ANDROIDPLOT");

            startActivity(openScreen);
        }
    });

   rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()  {
        public void onCheckedChanged(RadioGroup rGroup, int checkedId)  {

        String in = input.getText().toString();

           if (cel.isChecked()) {
               PROPERTY_NAME = "Fahrenheit";
               METHOD_NAME = "FahrenheitToCelsius";
               SOAP_ACTION  = "http://tempuri.org/FahrenheitToCelsius";                
           }
           else {
               PROPERTY_NAME = "Celsius";
               METHOD_NAME = "CelsiusToFahrenheit";
               SOAP_ACTION  = "http://tempuri.org/CelsiusToFahrenheit";
           }

           Convert(in);

        }            
    });        
}   

public void Convert(String val) {
     SoapObject requestddr = new SoapObject(WSDL_TARGET_NAMESPACE, "Vrati");

     PropertyInfo pi=new PropertyInfo();

        pi.setName(PROPERTY_NAME);
            pi.setValue(val);
            pi.setType(String.class);
            request.addProperty(pi);

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

        envelope.setOutputSoapObject(request);

        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);

        Object response= null;

        try {

        httpTransport.call(SOAP_ACTION, envelope);
        response = envelope.getResponse();

            }
        catch (Exception exception) {   

            response=exception;                 
            }

        result.setText(response.toString());

http://www.loginworks.com/technical-blogs/258-working-with-soap-web-service-through-android

于 2012-10-03T16:10:04.937 回答