2014-10-07 34 views
6

調用WSDL的Web服務,我想使用改裝在我的應用程序調用WSDL的web服務使用改裝的Android

這樣

public static String callWebservice(String username,String password) 
    { 

     String soapMessage="<?xml version=\"1.0\" encoding=\"utf-8\"?>"+ 
       "<Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\">"+ 
       "<Body>"+ 
       "<AuthenticateUser xmlns=\"http://tempuri.org/\">"+ 
       "<userName>"+username+"</userName>"+ 
       "<password>"+Helper.md5(password).toUpperCase(Locale.getDefault())+"</password>"+ 
       "</AuthenticateUser>"+ 
       "</Body>"+ 
       "</Envelope>"; 

     RequestInterceptor requestInterceptor = new RequestInterceptor() { 
       @Override 
       public void intercept(RequestFacade request) { 
       // request.addHeader("User-Agent", "Retrofit-Sample-App"); 
       request.addHeader("SOAPAction", "http://tempuri.org/IAuthenticate/AuthenticateUser"); 
       request.addHeader("Content-Type", "application/soap+xml;charset=utf-8"); 
       request.addHeader("Content-Type", "text/xml;charset=utf-8"); 
       } 
      }; 

      RestAdapter restAdapter = new RestAdapter.Builder() 
       .setEndpoint(Constants.WEBSERVICE_URL) 
       .setRequestInterceptor(requestInterceptor) 
       .build(); 

      LoginApi loginApi=restAdapter.create(LoginApi.class); 

      loginApi.getLogin(soapMessage,new Callback<String>() 
      { 

       @Override 
       public void success(String arg0, Response arg1) 
       { 

        Helper.Log("Response==>", arg0); 

       } 

       @Override 
       public void failure(RetrofitError arg0) 
       { 
        Helper.Log("error==>", arg0.toString()); 

       } 
      }); 

      return ""; 
     } 

但我正在逐漸例外

改造。 RetrofitError:400 Bad Request

如果我嘗試使用post請求的相同代碼爲按照,它正常工作。


StringBuffer stringResponse = new StringBuffer(); 
     try { 


      HttpURLConnection connection; 
      connection = (HttpURLConnection)new URL(Constants.WEBSERVICE_URL).openConnection(); 
      connection.setUseCaches(false); 
      connection.setDoOutput(true); 
      connection.setDoInput(true); 
      connection.setConnectTimeout(20000); 
      connection.setReadTimeout(20000); 
      connection.setRequestProperty("SOAPAction", "http://tempuri.org/IAuthenticate/AuthenticateUser"); 
      connection.setRequestProperty("Content-Type", "application/soap+xml;charset=utf-8"); 
      connection.setRequestProperty("Content-Type", "text/xml;charset=utf-8"); 
      //connection.setRequestProperty("Accept-Encoding", "gzip"); 
      connection.setRequestProperty("Content-Length", "" + soapMessage.length()); 
      connection.setFixedLengthStreamingMode(soapMessage.length()); 
      connection.setRequestMethod("POST"); 
      OutputStream os = connection.getOutputStream(); 
      os.write(soapMessage.getBytes("UTF-8"), 0, soapMessage.length()); 
      os.flush(); 
      os.close(); 
      int status = connection.getResponseCode(); 

      if(status==200) 
      { 

       BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()), 8 * 1024); 
       String line = null; 
       while ((line = reader.readLine()) != null) 
       { 
        stringResponse.append(line); 
       } 
       Helper.Log("Response==>", stringResponse.toString()); 
      } 
     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (UnsupportedEncodingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

請幫助...

+0

有您能使用改造來解決呢? – 2016-07-15 09:19:08

回答

-1

你爲什麼不嘗試KSOAP。它非常驚人。我對它進行了定製以適合我的應用,但是您可以從here訪問該庫。這裏是我如何使用我的自定義版本的ksoap,我喜歡打電話給minisoap。這裏是一個樣本

void fetch() { 
    MiniSoap soap_client = new MiniSoap(URL, NAMESPACE, METHOD); 
    soap_client.addProperty("", ""); 
    soap_client.addProperty("", ""); 
    //true for Dotnet and false for other webservices 
    soap_client.queryWS(IS_DOTNET, TIMEOUT); 
    //if you just wish to test/view results 
    System.out.println(soap_client.envelope.bodyIn); 
    // or you can get them from a list 
    List<String> responce = soap_client.full_report; 
    // or like this 
    List<String> response_2 = soap_client.results; 
    // or you can get the xml results 
    String xml_request = soap_client.androidHttpTransport.requestDump; 
    String xml_response = soap_client.androidHttpTransport.responseDump; 
} 

您可以從here得到minisoap的副本,我希望這不是爲時已晚。

1

這是可能使用翻新和簡單的XML庫中,可以看到here