2016-08-25 51 views
1

サーバーに要求を送信しようとしていますが、引数の1つにdateの型を設定する必要があります。タイプ日付を設定するには?今、私の要求はタイプstringです。私は、サーバーに送信していKsoap2を使用してサーバーに日付を送信します。

要求:要求送信用

<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:d="http://www.w3.org/2001/XMLSchema" 
    xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"> 
      <v:Header /><v:Body><n0:getOutlets id="o0" c:root="1" xmlns:n0="http://www.kltro.com"> 
      <userId i:type="d:string">a24f0c23-5f36-11e4-b332-984be174a0cc</userId> 
      <date i:type="d:string">2016-08-25</date> 
</n0:getOutlets> 
</v:Body> 
</v:Envelope> 

活動:

public class MainActivity extends AppCompatActivity { 

    private static final String NAMESPACE = "http://www.kltro.com"; 
    private static final String METHODNAME = "getOutlets"; 
    private static final String WSDL = "http://mapx.kashkan.org:5445/tro/ws/kltro"; 
    private static final String SOAP_ACTION = NAMESPACE + "#kltro:" + METHODNAME; 
    private static String TAG = "soap"; 

    public static String callWebservice() { 
     String responseDump = ""; 
     String requestDump=""; 

     try { 
      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

      SoapObject request = new SoapObject(NAMESPACE, METHODNAME); 
      request.addProperty("userId", "a24f0c23-5f36-11e4-b332-984be174a0cc"); 
      request.addProperty("date", "2016-08-25"); 

      envelope.bodyOut = request; 
      HttpTransportSE transport = new HttpTransportSE(WSDL); 
      transport.debug = true; 
      try { 
       transport.call(SOAP_ACTION, envelope); 
       requestDump = transport.requestDump; 
       responseDump = transport.responseDump; 

       Log.e(TAG, requestDump +"requestDump"); 
       Log.e(TAG, responseDump+"responseDump"); 
      } catch (IOException e) { 
       e.printStackTrace(); 

       Log.e(TAG, requestDump +"requestDump"); 
       Log.e(TAG, responseDump+"responseDump"); 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 

      Log.e(TAG, requestDump+"responseDump"); 
      Log.e(TAG, responseDump+"responseDump"); 
     } 

     Log.e("requestDump", requestDump); 
     Log.e(TAG, responseDump+"responseDump"); 
     return responseDump; 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 
     callWebservice(); 
    } 
} 

答えて

1

ksoap2-androidライブラリを通じて日付を送信するには、もう少し複雑です。 ksoap2-androidライブラリは、型の日付をシリアル化(またはマーシャリング)することを知らない。だから、カスタムMarshalクラスを与える必要があります。

しかし、幸いにもksoap2-androidライブラリにはすでにMarshalDateクラスがあります。ソースhereを参照してください。

このクラスをSoapSerializationEnvelopeに登録するだけです。

は、

private static final String NAMESPACE = "http://www.kltro.com"; 
private static final String METHODNAME = "getOutlets"; 
private static final String WSDL = "http://mapx.kashkan.org:5445/tro/ws/kltro"; 
private static final String SOAP_ACTION = NAMESPACE + "#kltro:" + METHODNAME; 
private static String TAG = "soap_tester"; 

public static String callWebservice() { 
    String responseDump = ""; 
    try { 
     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

     // Register the Date Marshal class 
     new MarshalDate().register(envelope); 

     SoapObject request = new SoapObject(NAMESPACE, METHODNAME); 
     request.addProperty("userId", "a24f0c23-5f36-11e4-b332-984be174a0cc"); 

     // Your date 
     String dateStr = "2016-08-25"; 

     // Parse the date into an object of type java.util.Date 
     SimpleDateFormat sdfIn = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); 

     // You might want to play around with the timezones 
     Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("ru"), Locale.ENGLISH); 
     calendar.setTime(sdfIn.parse(dateStr)); 

     // Clear the hour, minute and second 
     calendar.set(Calendar.HOUR, 0); 
     calendar.set(Calendar.MINUTE, 0); 
     calendar.set(Calendar.SECOND, 0); 
     Date date = calendar.getTime();; 

     // Create a property with the date object and type as java.util.Date.class 
     PropertyInfo dateProperty = new PropertyInfo(); 
     dateProperty.setValue(date); 
     dateProperty.setName("date"); 
     dateProperty.setType(Date.class); 

     // Add it to the request 
     request.addProperty(dateProperty); 

     envelope.bodyOut = request; 
     HttpTransportSE transport = new HttpTransportSE(WSDL); 
     transport.debug = true; 
     try { 
      transport.call(SOAP_ACTION, envelope); 
      String requestDump = transport.requestDump; 
      responseDump = transport.responseDump; 
      Log.e(TAG, requestDump); 
      Log.e(TAG, responseDump); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return responseDump; 
} 

を以下のコードをチェックアウトこれは助けに感謝requestDump

<?xml version="1.0" encoding="UTF-8"?> 
<v:Envelope xmlns:v="http://schemas.xmlsoap.org/soap/envelope/" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
    <v:Header /> 
    <v:Body> 
     <n0:getOutlets xmlns:n0="http://www.kltro.com" id="o0" c:root="1"> 
     <userId i:type="d:string">a24f0c23-5f36-11e4-b332-984be174a0cc</userId> 
     <date i:type="d:dateTime">2016-08-24T00:00:00.000Z</date> 
     </n0:getOutlets> 
    </v:Body> 
</v:Envelope> 

幸運:)

+0

です! –

関連する問題