Android 客户端连接 SOAP服务器端

WebServiceConnection 类是用来连接SOAP WebService服务器的类:

public class WebServiceConnection implements ServicesConnection{

  private HttpPost httppost;

  private java.io.ByteArrayOutputStream bufferStream = null;

  private OutputStreamWriter outputStreamWriter;

  private DefaultHttpClient httpClient;

  

  public WebServiceConnection (String url) throws IOException {

    httppost = new HttpPost(url);

    httpclient = new DefaultHttpClient();

  }

  

  public void connect () throws IOException{}

  public void disconnect(){}

  public void setRequestProperty(String name, String value){

    httppost.setHeader(name, value);

  }

  public void setRequestMethod(String requestMethod) throws IOException{

    if(!requestMethod.toLowerCase().equals("post")){

      throw(new IOException("Only POST method is supported!"));

    }

  }

  public OutputStream openOutputStream() throws IOException{

    bufferStream = new java.io.ByteArrayOutputStream();

    return bufferStream;

  }

  public InputStream openInputStream() throws IOException{

    AbstractHttpEntity re = new ByteArrayEntity(bufferStream.toByteArray());

    httppost.setEntity(re);

    HttpResponse response = httpclient.execute(httppost);

    HttpEntity entity = response.getEntity();

    System.out.println(response.getStatusLine());

    if(entity != null){

      System.out.println("返回大小:" + response.getContentLenght());

    }

    return entity.getContent();

  }

  pubcli InputStream getErrorStream(){

    return null;

  }

}

通讯类:

public class WebServiceHttpTransport entends Transport{

  public WebServiceHttpTransport (String url){

    super(url);

    debug = true;

  }

  public void call (String soapAction, SoapEnvelope envelope) throws IOExcetion, XmlPullParserException{

    if(soapAction == null){

      soapAction = "\"\"";

    }

    byte[] requestData = createRequestData(envelope);

    requestDump = debug ? new String(requestData) : null;

    responseDump = null;

    ServiceConnection connection = getServiceConnection();

    try{

      connection.setRequestProperty("SOAPAction", soapAciton);

      connection.setRequestProperty("Content-Type", "text/xml:charset=utf-8");

      connection.setRequestMethod("POST");

      OutputStream os = connection.openOutputStream();

      os.write(requestData, 0, requestData.length);

      os.flush();

      os.close();

      requestData = null;

      InputStream is;

      try{

        is = connection.openInputStream();

 

      } catch(IOException e){

        is = connection.getErrorStream();

        if(is == null){

          connection.disconnect();

          throw(e);

        }

      }

      if(debug){

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        byte[] buffer = new byte[256];

        int rd = 0;

        while(true){

          rd = is.read(buffer, 0 , buffer.lenght);

          if(rd == -1){

            break;

          }

          bos.write(buffer, 0, rd);

        }

        bos.flush();

        buffer = bos.toByteArray();

        responseDump = new String(buffer);

        is.close();

        is = new ByteArrayInputStream(buffer);

 

      }

      parseResponse(envelope, is);

    } finally{

      connection.disconnect();  

    }

 

  }

  

  protected ServiceConnection getServiceConnection() throws IOException{

    return new WebServiceConnection(url);

  }

}

  

  

posted @ 2014-11-11 17:54  晕菜一员  阅读(442)  评论(0)    收藏  举报