在Android中使用KSOAP连接错误的Web服务

时间:2012-11-08 13:33:55

标签: android web-services

我正在尝试使用kso​​ap2从android连接到.net webservice,但我得到的输出为"null°F" ;.请找到下面的日志文件并帮我清除这个。我使用webservice中的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;
    }

}

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

asynctask类定义中有一个String输入。但是你没有把这个字符串给你的asynctask类。 两种方式:

1 - 从定义中删除String参数

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

|

|

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

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

new servicecall().execute("Some string");