0

我正在尝试使用 ksoap2 从 android 连接到 .net Web 服务,但我得到空输出为"null°F";。请在下面找到日志文件并帮助我清除它。我使用来自网络服务的 KSOAP

package com.example.webservice;

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

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class WebService extends Activity {
       private final String NAMESPACE = "http://tempuri.org/";
       private final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
       private final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
       private final String METHOD_NAME = "CelsiusToFahrenheit";
    Button b;
    TextView tv;
    EditText et;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_service);
        et=(EditText)findViewById(R.id.editText1);
        tv=(TextView)findViewById(R.id.Result);
        b=(Button)findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            String result=getFarenheit(et.getText().toString());
            tv.setText(result+"°F");
            }
        });
    }
    public String getFarenheit(String celsius){
        SoapObject request= new SoapObject(NAMESPACE, METHOD_NAME);
        PropertyInfo celsuiusPI= new PropertyInfo();
        celsuiusPI.setName("Celsius");
        celsuiusPI.setValue(celsius);
        celsuiusPI.setType(double.class);
        request.addProperty(celsuiusPI);
        SoapSerializationEnvelope envelope=new SoapSerializationEnvelope (SoapEnvelope.VER11);
        envelope.dotNet=true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport=new HttpTransportSE(URL);
        try{
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive response=(SoapPrimitive)envelope.getResponse();
            Log.i("WebService output", response.toString());
            return response.toString();

        }
        catch(Exception e){
            e.printStackTrace();
        }
        //return null;
        return null;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_web_service, menu);
        return true;
    }

}

有人能说出我程序中的错误吗

4

2 回答 2

1

空指针异常是通用的方法,您必须指定/提供更多调试信息。您的网址也可能是错误的,请尝试?wsdl在其末尾添加。

于 2012-11-08T13:40:20.827 回答
0

在您的 asynctask 类定义中有一个字符串输入。但是您没有将该字符串提供给您的 asynctask 类。两种方式:

1-从定义中删除字符串参数

public class servicecall extends AsyncTask<String,Void,String>

|

|

 public class servicecall extends AsyncTask<Void,Void,String>

2-给你的 asynctask 类一些字符串

new servicecall().execute("Some string");
于 2012-11-08T14:18:43.070 回答