2012-11-22 89 views
0

我有一個Web服務,需要一個SOAP請求來對String執行一個簡單的方法。現在我在Android上編寫應用程序來使用這個Web服務,我希望它能序列化一些數據並通過SOAP發送給Web服務。從Android發送數據到Web服務(通過SOAP)

下面是一些代碼:

public class SendedLocation implements Serializable { 
    public String MESSAGE; 

    public SendedLocation() { 
    } 

    public SendedLocation(int mId, float mLongitude, float mLatitude) { 

     MESSAGE = String.valueOf(mId) + ";" + String.valueOf(mLongitude) + ";" + String.valueOf(mLatitude); 
    } 

    public Object getProperty(int arg0) { 

     switch (arg0) { 
     case 0: 
      return MESSAGE; 
     } 

     return null; 
    } 

    public int getPropertyCount() { 
     return 1; 
    } 

    public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) { 
     switch (index) { 
     case 0: 
      info.type = PropertyInfo.STRING_CLASS; 
      info.name = "MESSAGE"; 
      break; 
     default: 
      break; 
     } 
    } 

    public void setProperty(int index, Object value) { 
     switch (index) { 
     case 0: 
      MESSAGE = value.toString(); 
      break; 
     default: 
      break; 
     } 
    } 

} 

public void callWebService(int ID, float Longitude, float Latitude) { 
    try { 
     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
     AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
       URL); 

     SendedLocation mSended = new SendedLocation(ID, Longitude, Latitude); 

     PropertyInfo p1 = new PropertyInfo(); 
     p1.setName("mSended"); 
     p1.setValue(mSended); 
     p1.setType(mSended.getClass()); 
     request.addProperty(p1); 

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
       SoapEnvelope.VER11); 

     envelope.dotNet = true; 
     envelope.setOutputSoapObject(request); 

     envelope.addMapping(NAMESPACE, "mSended", mSended.getClass()); 

     MarshalString marshalString = new MarshalString(); 

     marshalString.register(envelope); 

     // Make the soap call. 
     androidHttpTransport.call(SOAP_ACTION, envelope); 

     Object results = (Object) envelope.getResponse(); 
     // to get the data String 
     // resultData=result.getProperty(0).toString(); 
     String temp = results.toString(); 
     System.out.println(temp); 
    } catch (Exception aE) { 
     System.out.println(aE.toString()); 
    } 
} 
public class MarshalString implements Marshal 
{ 


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

     return String.valueOf(parser.nextText()); 
    } 


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

    } 


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

} 

,我調用中的onCreate)這個callWebService()方法(是這樣的:

callWebService(ID , (float)location.getLongitude() , (float)location.getLatitude()); 

然後,當我運行我的應用程序從得到修復GPS但發送數據做網絡服務,它給了我:

java.lang.RuntimeException: Cannot serialize... 

有人可以向我解釋什麼是s我應該添加它使它工作嗎?我真的不知道,試圖線索使用Marshal類後...

回答

2

嘗試使用SOAP信封用於發送數據, 語法SOAP信封:

final String envelope = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+ 
         "<SOAP-ENV:Envelope SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" " + 
         "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" " + 
         "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " + 
         "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + 
         " xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" " + 
         "xmlns:tns=\"urn:registerwsdl\">"+ 
         "<SOAP-ENV:Body>"+ 
         "<tns:register " + 
         "xmlns:tns=\"urn:registerwsdl\">"+ 
    "<your_feild_name xsi:type=\"xsd:string\">"+"your_value"+"</your_feild_name>"+ 



         "</tns:register>"+ 

           // "</SOAP-ENV:Body></SOAP-ENV:Envelope>",Name,Email,Password,Status,Type,Date]; 
           "</SOAP-ENV:Body></SOAP-ENV:Envelope>"; 

,然後在這個函數中使用這個信封, 可以傳遞多個值與SOAP信封

String CallWebService(String url, 
      String soapAction, 
      String envelope) { 
      final DefaultHttpClient httpClient=new DefaultHttpClient(); 
      // request parameters 

      HttpParams params = httpClient.getParams(); 
      HttpConnectionParams.setConnectionTimeout(params, 20000); 
      HttpConnectionParams.setSoTimeout(params, 25000); 
      // set parameter 
      HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true); 

      // POST the envelope 
      HttpPost httppost = new HttpPost(url); 
      // add headers 
      httppost.setHeader("soapaction", soapAction); 
      httppost.setHeader("Content-Type", "text/xml; charset=utf-8"); 

      String responseString=""; 
      try { 

       // the entity holds the request 
      HttpEntity entity = new StringEntity(envelope); 
      httppost.setEntity(entity); 

      // Response handler 

      ResponseHandler<String> rh=new ResponseHandler<String>() { 
      // invoked when client receives response 

       public String handleResponse(HttpResponse response) 
       throws ClientProtocolException, IOException { 

      // get response entity 
      HttpEntity entity = response.getEntity(); 


      // read the response as byte array 
        StringBuffer out = new StringBuffer(); 
        byte[] b = EntityUtils.toByteArray(entity); 

        // write the response byte array to a string buffer 
        out.append(new String(b, 0, b.length)); 

        return out.toString(); 
      } 
      }; 

      responseString=httpClient.execute(httppost, rh); 

      } 
      catch (Exception e) { 
       Log.v("exception", e.toString()); 
      } 

      xml = responseString.toString(); 
      // close the connection 
      System.out.println("xml file ------"+xml); 
      httpClient.getConnectionManager().shutdown(); 
      return responseString; 
     } 

並且在最後通過使用任何XML解析器解析XML的輸出。

+0

非常感謝,它的作品只是輝煌!:) –

+0

歡迎:) – Ravinder

+0

@Ravinder:你有任何關於傳遞Arraylist到肥皂服務在android的想法。請。請參考此[http://stackoverflow.com/questions/19198017/pass-arraylist-data-into-soap-web-service-in-android](http://stackoverflow.com/questions/19198017/pass-arraylist- data-into-soap-web-service-in-android) –

相關問題