-->

Web service provides output “Error” when i access

2019-08-04 19:07发布

问题:

Sorry im new to android development and i am trying to access a web service though kSoup2...

The code is as follows

    package com.example.mytestws;

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

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


public class MainActivity extends Activity {

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

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

        tv=(TextView) findViewById(R.id.textView1);
        SoapObject Request=new SoapObject(NAMESPACE, METHOD_NAME);
        Request.addProperty("Celcius", "32");
        SoapSerializationEnvelope soapEnvelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
        soapEnvelope.dotNet=true;
        soapEnvelope.setOutputSoapObject(Request);

        AndroidHttpTransport aht=new AndroidHttpTransport(URL);

        try{
            aht.call(SOAP_ACTION, soapEnvelope);
            SoapPrimitive resultString=(SoapPrimitive) soapEnvelope.getResponse();
            tv.setText(resultString.toString());
        }
        catch(Exception e){

            //e.printStackTrace();
        }


    }

}

and the manifest is as follows:

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

    <uses-sdk
        android:minSdkVersion="1"
        android:targetSdkVersion="7" />
    <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.mytestws.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>

I am receiving the output as "Error" instead of the actual string returned through the web service.. Can you please help me sort this problem out?

回答1:

Defined the following code which is:

SoapPrimitive resultString=(SoapPrimitive) soapEnvelope.getResponse();

as the following:

SoapObject resultString = (SoapObject)envelope.bodyIn;

and do the follwoing after that:

if(resultString != null)
                {
                      tv.setText(resultString.toString());
                }

and also do not do any network operation in main thread, instead you can do it in AsyncTask.



回答2:

Thanks figured it out. Simple spelling mistake at:

Request.addProperty("Celcius", "32");

instead it should be:

Request.addProperty("Celsius", "32");