2012-11-08 72 views
0

我想從android使用kso​​ap2連接到.net web服務,但我收到輸出爲"null°F";。請在下面找到日誌文件並幫助我清除此問題。我用KSOAP從web服務使用KSOAP連接錯誤的Web服務android

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

} 

有人能告訴故障在我的計劃

+0

我發佈一個完整的例子與Android 2_4工作: http://stackoverflow.com/a/13829138/1895995 – Vik

回答

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"); 
1

一個空指針異常的方式仿製,你必須指定/提供更多的調試信息。 此外,您的網址可能有誤,請在結尾處添加?wsdl

+0

我也認爲這將是解決方案。由於KSOAP需要獲取wsdl的URL。 – andunslg