2017-03-07 13 views
-1

私はSOAPの新しい開発者ですので、どのように私がアンドロイドでsoap apiを呼び出すことができますか教えてください。 ここに私が見つけたが、それがどのように動作するのかわからない方法です。私はthisライブラリアンドロイドとSOAP apiの接続

public static String connectSOAP(String url, String soapNamespace, String soapAction, String soapMethod, Hashtable<String, String> HT) { 

     String result = null; 

     SoapObject request = new SoapObject(soapNamespace, soapMethod); 

     if (HT != null) { 
      Enumeration<String> en = HT.keys(); 
      while (en.hasMoreElements()) { 

       Object k = en.nextElement(); 
       Object v = HT.get(k); 
       request.addProperty(k.toString(), v); 
       System.out.println("key = " + k.toString() + "; value = " + v); 
      } 
     } 


     SoapSerializationEnvelope envelope = 
       new SoapSerializationEnvelope(SoapEnvelope.VER11); 

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

     HttpTransportSE androidHttpTransport = new HttpTransportSE(url); 
     //envelope.getResponse(); 


     try { 

      androidHttpTransport.call(soapAction, envelope); 

      if (envelope.bodyIn instanceof SoapFault) { 

       result = ((SoapFault) envelope.bodyIn).faultstring; 


      } else { 

       SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn; 
       result = resultsRequestSOAP.getProperty(0).toString(); 
      } 


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

これは私がAPI呼び出しとして使用したいが、私はこれを使用する方法を知らない方法であるをダウンロードしています。 だから私を助けてください。

+0

[ここ](https://code.tutsplus.com/tutorials/consuming-web-services-with-ksoap--mobile-21242)を順を追って説明します。 – sodhankit

+0

よろしくお願いします@sodhankit。私は何か問題がある場合は、この時点に戻ります。 –

答えて

0

soap webserviceを使用する際には、soap要求を行うためにライブラリを使用しないことを強くお勧めします。アンドロイドによって提供されるクラスを使用する方が、より便利でエラーの発生が少なくなります。これは、ライブラリを使用せずにSOAPリクエストを行う方法です。まず、WindowsアプリケーションであるSOAP Uiの使い方を知る必要があります。ここでwsdlファイルをインポートすることができます。構文が正しい場合は、Webサービスのリクエスト本文を示す画面が表示されます。テスト値を入力すると、応答構造が得られます。このリンクはandroiodコードに今すぐに石鹸UI https://www.soapui.org/soap-and-wsdl/working-with-wsdls.html

の使用方法についてご案内します。

私たちは、非同期タスクを拡張runTaskという名前のクラスを作成し、リクエストボディを送信し、要求応答を取得するためにHTTPを使用します:

private class runTask extends AsyncTask<String, String, String> { 

     private String response; 
     String string = "your string parameter" 
     String SOAP_ACTION = "your soap action here"; 

     String stringUrl = "http://your_url_here"; 
     //if you experience a problem with url remove the '?wsdl' ending 



     @Override 
     protected String doInBackground(String... params) { 

      try { 

         //paste your request structure here as the String body(copy it exactly as it is in soap ui) 
         //assuming that this is your request body 


       String body = "<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">" + 
           "<soap:Body>"+ 
           "<GetCitiesByCountryResponse xmlns="http://www.webserviceX.NET">"+ 
           "<GetCitiesByCountryResult>"+string+"</GetCitiesByCountryResult>"+ 
           "</GetCitiesByCountryResponse>"+ 
           "</soap:Body>"+ 
           "</soap:Envelope>"; 


       try { 
        URL url = new URL(stringUrl); 
        HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
        conn.setRequestMethod("POST"); 
        conn.setDoOutput(true); 
        conn.setDefaultUseCaches(false); 
        conn.setRequestProperty("Accept", "text/xml"); 
        conn.setRequestProperty("SOAPAction", SOAP_ACTION); 

        //push the request to the server address 

        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
        wr.write(body); 
        wr.flush(); 

        //get the server response 

        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
        StringBuilder builder = new StringBuilder(); 
        String line = null; 

        while ((line = reader.readLine()) != null) { 


         builder.append(line); 
         response = builder.toString();//this is the response, parse it in onPostExecute 

        } 


       } catch (Exception e) { 

        e.printStackTrace(); 
       } finally { 

        try { 

         reader.close(); 
        } catch (Exception e) { 

         e.printStackTrace(); 
        } 
       } 


      } catch (Exception e) { 

       e.printStackTrace(); 
      } 

      return response; 
     } 

     /** 
     * @see AsyncTask#onPostExecute(Object) 
     */ 
     @Override 
     protected void onPostExecute(String result) { 



      try { 

       Toast.makeText(this,"Response "+ result,Toast.LENGTH_LONG).show(); 

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

今だけクラスを実行し、魔法が起こるのを見る:

runTask runner = new runTask(); 
runner.execute(); 

あなたの応答を取得した後、あなたはパーをすることができますそれはDOMやSAXの解析、いくつかの小さな研究を使用して、あなたは解析する方法を理解するでしょう。 Javaを使用してWSDL Webサービスを作成し、Eclipse Ideを使用することができます。チュートリアルは、youtube上のWSDL Webサービスでも利用できます。

解決策がわからない場合は、お気軽にご相談ください。

関連する問題