android - 我的 Activity 没有打印 web 服务的结果,而是只写 "TextView"

标签 android web-services ksoap2 soap-client android-ksoap2

我希望我的练习应用程序使用 w3schools 的 soap 网络服务并将摄氏温度转换为华氏温度,并在 TextView 中显示转换后的温度。但它只是在屏幕上写“TextView”。 LogCat 只是说,

"Emulator without GPU detected."

我该怎么办?

 package com.falafel.MyTest;
//ANDROID CLIENT OF SOAP WEB SERVICES. SAMPLE (YOUTUBE) CLIENT.
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.widget.TextView;


public class FirstScreen extends Activity {

    private static final String SOAP_ACTION= "http://www.w3schools.com/webservices/CelsiusToFahrenheit"; //soapAction will point to where the namespace is, slash, the name of the method
    private static final String NAMESPACE = "http://www.w3schools.com/webservices/";
    private static final String METHOD_NAME= "CelsiusToFahrenheit";
    private static final String URL= "http://www.w3schools.com/webservices/tempconvert.asmx";//This URL is very important. Make sure it is pointing to asmx, and not to wsdl, that is a complete URL to where the web service itself is; This is not a WSDL file.

    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.textView1);
        String[] params= {SOAP_ACTION, NAMESPACE, METHOD_NAME, URL};
        new MyTask().execute(params);
    }

        private class MyTask extends AsyncTask<String, Void, SoapPrimitive>{

            @Override
            protected SoapPrimitive doInBackground(String... params) {
                //Soap Portion of this client program starts here. This is 1/3rd of the major functionality of this program, since there are three major portions.
                SoapObject Request = new SoapObject(params[1], params[2]);//If we CelsiusToFahrenheit dint take any parameters, I would be done with my soap request by this line. But it does take a string parameter (as we say in the wsdl), so we need to package the required parameters into the Request. That is, next line!
                Request.addAttribute("Celsius", "32"); //Celcius is the name of the parameter required by the function. 32 --> we are giving a hardcoded value here for simplicity. | We have to add this line for each required parameter.


                //Here starts the 2nd out of 3 major portions of this program. This portion is the SoapSerializationEnvelope, which is an important piece of what needs to happen in ksoap to call web services inside of android.
                SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); //An important thing in creating this object is specifying which version of the soap specification we want to use. This is version 1.1
                                                                                                    //soapEnvelope is an in and out object, which means that it has 2 types of information clusters, named bodyIn which contains information we send to web service while making the call (i.e. the xml going out), and named bodyOut which brings back, from the web service, the response of the call to the web service (that is the xml coming in).
                soapEnvelope.dotNet= true; //Remember the asmx in the URL, that is this service is based in ASP.Net, and not PHP or something, so you have to specify that. If you forget to specify that, you'll get some errors complaining that some of your objects are NULL. And the tricky part is that sometimes it will work, so yeah! If you are sure that yours is a dotNet based web service, always be safe by setting it to true, and if it is not dotNet based, you'll have to set it to nottrue
                soapEnvelope.setOutputSoapObject(Request); //Pass the packaged Request to SoapEnvelope itself. SetOutputSoapObject() assigns the object to the envelope as the outbound message for the soap call.


                //3rd of the 3 major parts of this program:- Transport (HTTP transport itself)
                HttpTransportSE hts= new HttpTransportSE(params[3]);
                SoapPrimitive resultString=new SoapPrimitive("sorry","sorry","sorry");
                try{   //try-catch block to actually make the call
                    hts.call(params[0], soapEnvelope); //SOAP_ACTION contains where the web service is, appended with the function to be invoked. While all the information which needs to be sent to the web service is present (like Request) in the bodyIn portion of the soapEnvelope. After the call is successful (i.e. after the execution of this line, the bodyOut portion of this envelope will contain the response of the webservice.
                    resultString = (SoapPrimitive) soapEnvelope.getResponse();
                    //tv.setText("Status : " + resultString);
                }   
                catch (Exception e){
                    e.printStackTrace();
                }
                return resultString;
            }

            @Override 
            protected void onPostExecute(SoapPrimitive resultString){
                tv.setText("Status: " + resultString.toString());
            }
    }
}

编辑:-

我在实现前两个答案后编辑了上面的代码。但结果第一次是“Status:False”,然后又是屏幕上的“TextView”。

此外,更令人困惑的是,尽管我在两个不同的模拟器上对其进行了测试,但以下是 LogCat 的全部内容:

04-04 04:08:56.355: D/gralloc_goldfish(1115): Emulator without GPU emulation detected.
04-04 04:08:57.705: D/dalvikvm(1115): GC_FOR_ALLOC freed 86K, 5% free 3088K/3244K, paused 65ms, total 73ms
04-04 04:08:59.905: D/dalvikvm(1115): GC_FOR_ALLOC freed 405K, 14% free 3185K/3664K, paused 52ms, total 52ms

编辑:-

它再次打印“Status:False”。这是我认为捕获的异常。但为什么会出现这种不可预测的行为?

最佳答案

您已经声明了 MyTask 但您正在执行它吗?我想你错过了类似的东西

new MyTask().execute(params); 

在您的 onCreate 中。

至于“未检测到 GPU 的模拟器”,这与您面临的问题没有任何关系。

除此之外,您的代码还存在一些其他问题。例如,在您的 onPostExecute 中,您可能需要调用 .toString()resultString

关于android - 我的 Activity 没有打印 web 服务的结果,而是只写 "TextView",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22856295/

相关文章:

c# - C# 中的 Web 服务和属性

Android KSoap 在 HttpTransportSE 上获取 java.io.FileNotFoundException 并抛出 XmlPullParserException

android - 如何在 android 上使用 ksoap2 调用 WCF 服务?

android - 我在哪里可以找到默认的 android xml 绘图?

Android HelloOpenGLES20 示例不起作用

android - 如何删除vcf文件中的重复联系人

php - 将 SOAP XML 响应转换为 PHP 对象或数组

php 在一台机器上解析失败,但在另一台机器上解析失败

java - 在 Firestore : detect if the operation is successful on the server, 本地存储文档还是不成功?

android - 从 Android 客户端将参数传递到 Windows Azure 上托管的 VB.NET WS