2011-07-07 51 views
2

在我的應用程序我想通過double valueweb service使用ksoap但我得到NPE。在代碼「exit_distance」是double值。任何機構可以找到的錯誤,並寄樣品例如android-如何通過雙倍的價值服務使用kso​​ap

代碼在這裏

//valus required for test 
RB_Constant.RB_Webservice_URL = ? 
RB_Constant.RB_Webservice_Namespace = ? 

public String getExitsRestaurants() throws SoapFault 
{   
    String data = ""; 
    String serviceUrl = RB_Constant.RB_Webservice_URL; 
    String serviceNamespace = RB_Constant.RB_Webservice_Namespace; 
    String soapAction = "http://www.roadbrake.com/GetExitDetailsExtn"; 
    String type_of_soap = "GetExitDetailsExtn";  

    try 
    { 
     SoapObject Request = new SoapObject(serviceNamespace, type_of_soap); 

     PropertyInfo HighwayIdObj = new PropertyInfo(); 
     HighwayIdObj.name = "HighwayId"; 
     HighwayIdObj.type = PropertyInfo.INTEGER_CLASS; 

     Request.addProperty("ExitNo", 7); 
     Request.addProperty(HighwayIdObj, highwayid); 
     Request.addProperty("HighwayName", 95); 
     Request.addProperty("exit_distance", 1.2);      


     System.out.println("Request Value->"+Request.toString()); 

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.dotNet = true;   

     MarshalDouble md = new MarshalDouble(); 
     md.register(envelope); 

     envelope.setOutputSoapObject(Request); 

     try 
     { 
      HttpTransportSE androidHttpTransport = new HttpTransportSE(serviceUrl); 
      androidHttpTransport.call(soapAction, envelope); 
     } 
     catch(Exception e) 
     { 
      System.out.println("Webservice calling error ->"+e.toString()); 
     } 

     SoapPrimitive response = (SoapPrimitive)envelope.getResponse(); 
     data = response.toString(); 
     System.out.println("web service response->"+response.toString()); 
    } 
    catch(Exception e) 
    { 
     System.out.println("Soap Method Error ->"+e.toString());  
    }   
    return data; 
} 

public class MarshalDouble implements Marshal 
{ 

    @Override 
    public Object readInstance(XmlPullParser parser, String namespace, String name, 
      PropertyInfo expected) throws IOException, XmlPullParserException { 

     return Double.parseDouble(parser.nextText()); 
    } 


    public void register(SoapSerializationEnvelope cm) { 
     cm.addMapping(cm.xsd, exit_distance, Double.class, this); 

    } 

    @Override 
    public void writeInstance(XmlSerializer writer, Object obj) throws IOException { 
      writer.text(obj.toString()); 
     }   
} 

回答

0

與NPE堆棧跟蹤應該給你它發生在哪裏的行號。只需在那裏設置一個調試中斷點,並查看自己什麼是null。

0

最後我解決了這個問題。問題在MarshalDouble類的register()方法中。以下是解決這個問題的方法。

public void register(SoapSerializationEnvelope cm) { 
    cm.addMapping(cm.xsd, "double", Double.class, this);   
} 
2

要確切使用這樣

Marshal類

import org.ksoap2.serialization.Marshal; 
import org.ksoap2.serialization.PropertyInfo; 
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.xmlpull.v1.XmlPullParser; 
import org.xmlpull.v1.XmlPullParserException; 
import org.xmlpull.v1.XmlSerializer; 

import java.io.IOException; 


public class MarshalDouble implements Marshal { 
    public Object readInstance(XmlPullParser parser, String namespace, String name, 
           PropertyInfo expected) throws IOException, XmlPullParserException { 

     return Double.parseDouble(parser.nextText()); 
    } 


    public void register(SoapSerializationEnvelope cm) { 
     cm.addMapping(cm.xsd, "double", Double.class, this); 

    } 


    public void writeInstance(XmlSerializer writer, Object obj) throws IOException { 
     writer.text(obj.toString()); 
    } 
} 

實施

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
envelope.implicitTypes = true; 
envelope.dotNet = true; 
envelope.encodingStyle = SoapSerializationEnvelope.XSD; 
envelope.setOutputSoapObject(request); 

**MarshalDouble md = new MarshalDouble(); 
md.register(envelope);** 
1

,你可以簡單地設置的PropertyInfo類型是雙.class

SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME); 
    PropertyInfo pi=new PropertyInfo(); 

    pi.setName("name"); 
    pi.setValue(a); 
    pi.setType(a.getClass()); 
    request.addProperty(pi); 

    pi=new PropertyInfo(); 
    pi.setName("latitude"); 
    pi.setValue(b); 
    pi.setType(Double.class); 
    request.addProperty(pi); 

    pi=new PropertyInfo(); 
    pi.setName("longitude"); 
    pi.setValue(c); 
    pi.setType(Double.class); 

    request.addProperty(pi); 

      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
      SoapEnvelope.VER11); 
      envelope.dotNet = true; 
相關問題