本文介绍了Web服务提供输出"错误"当我访问它通过机器人而不是提供预期的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起进出口新的android开发,我试图访问虽然kSoup2 ...

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

在code是如下:

    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();
        }


    }

}

和清单如下:

    <?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>

我收到的输出为错误,而不是实际的字符串通过Web服务返回..你可以帮我解决这问题了呢?

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?

推荐答案

定义如下code是:

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

如下:

SoapObject resultString = (SoapObject)envelope.bodyIn;

和之后做follwoing:

and do the follwoing after that:

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

和还没有做在主线程任何网络操作,而是可以在AsyncTask的做到这一点。

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

这篇关于Web服务提供输出&QUOT;错误&QUOT;当我访问它通过机器人而不是提供预期的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 02:11