Click here to Skip to main content
15,886,075 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can I connect the android app and web service



public class HttpTransportSE_Mnl extends Transport {

private ServiceConnection serviceConnection;

/**
* Creates instance of HttpTransportSE with set url
*
* @param url
* the destination to POST SOAP data
*/
public HttpTransportSE_Mnl(String url) {
super(null, url);
}

/**
* Creates instance of HttpTransportSE with set url and defines a
* proxy server to use to access it
*
* @param proxy
* Proxy information or null for direct access
* @param url
* The destination to POST SOAP data
*/
public HttpTransportSE_Mnl(Proxy proxy, String url) {
super(proxy, url);
}

/**
* Creates instance of HttpTransportSE with set url
*
* @param url
* the destination to POST SOAP data
* @param timeout
* timeout for connection and Read Timeouts (milliseconds)
*/
public HttpTransportSE_Mnl(String url, int timeout) {
super(url, timeout);
}

public HttpTransportSE_Mnl(Proxy proxy, String url, int timeout) {
super(proxy, url, timeout);
}

/**
* Creates instance of HttpTransportSE with set url
*
* @param url
* the destination to POST SOAP data
* @param timeout
* timeout for connection and Read Timeouts (milliseconds)
* @param contentLength
* Content Lenght in bytes if known in advance
*/
public HttpTransportSE_Mnl(String url, int timeout, int contentLength) {
super(url, timeout);
}

public HttpTransportSE_Mnl(Proxy proxy, String url, int timeout, int contentLength) {
super(proxy, url, timeout);
}

/**
* set the desired soapAction header field
*
* @param soapAction
* the desired soapAction
* @param envelope
* the envelope containing the information for the soap call.
* @throws IOException
* @throws XmlPullParserException
*/
public void call(String soapAction, SoapEnvelope envelope) throws IOException, XmlPullParserException {

call(soapAction, envelope, null);
}

public List call(String soapAction, SoapEnvelope envelope, List headers)
throws IOException, XmlPullParserException {
//return call(soapAction, envelope, headers, null);
return headers;
}

/**
* Perform a soap call with a given namespace and the given envelope providing
* any extra headers that the user requires such as cookies. Headers that are
* returned by the web service will be returned to the caller in the form of a
* List of HeaderProperty instances.
*
* @param soapAction
* the namespace with which to perform the call in.
* @param envelope
* the envelope the contains the information for the call.
* @param headers
* List of HeaderProperty headers to send with the SOAP request.
* @param outputFile
* a file to stream the response into rather than parsing it, streaming happens when file is not null
*
* @return Headers returned by the web service as a List of
* HeaderProperty instances.
* @throws Exception
*/
public String call(String soapAction, SoapEnvelope envelope, List headers, File outputFile,int intSegId,boolean isFileWrite)
throws Exception {
String strOutput=null;
try
{
if (soapAction == null) {
soapAction = "\"\"";
}

byte[] requestData = createRequestData(envelope, "UTF-8");

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

ServiceConnection connection = getServiceConnection();

connection.setRequestProperty("User-Agent", USER_AGENT);
// SOAPAction is not a valid header for VER12 so do not add
// it
// @see "http://code.google.com/p/ksoap2-android/issues/detail?id=67
if (envelope.version != SoapSerializationEnvelope.VER12) {
connection.setRequestProperty("SOAPAction", soapAction);
}

if (envelope.version == SoapSerializationEnvelope.VER12) {
connection.setRequestProperty("Content-Type", CONTENT_TYPE_SOAP_XML_CHARSET_UTF_8);
} else {
connection.setRequestProperty("Content-Type", CONTENT_TYPE_XML_CHARSET_UTF_8);
}

connection.setRequestProperty("Connection", "close");
connection.setRequestProperty("Accept-Encoding", "gzip");
connection.setRequestProperty("Content-Length", "" + requestData.length);
connection.setFixedLengthStreamingMode(requestData.length);

// Pass the headers provided by the user along with the call
if (headers != null) {
for (int i = 0; i < headers.size(); i++) {
HeaderProperty hp = (HeaderProperty) headers.get(i);
connection.setRequestProperty(hp.getKey(), hp.getValue());
}
}

connection.setRequestMethod("POST");


OutputStream os = connection.openOutputStream();

os.write(requestData, 0, requestData.length);
os.flush();
os.close();
requestData = null;
InputStream is;
List retHeaders = null;
byte[] buf = null; // To allow releasing the resource after used
int contentLength = 8192; // To determine the size of the response and adjust buffer size
boolean gZippedContent = false;

// try {
retHeaders = connection.getResponseProperties();
for (int i = 0; i < retHeaders.size(); i++) {
HeaderProperty hp = (HeaderProperty)retHeaders.get(i);
// HTTP response code has null key
if (null == hp.getKey()) {
continue;
}
// If we know the size of the response, we should use the size to initiate vars
if (hp.getKey().equalsIgnoreCase("content-length") ) {
if ( hp.getValue() != null ) {
try {
contentLength = Integer.parseInt( hp.getValue() );
} catch ( NumberFormatException nfe ) {
contentLength = 8192;
}
}
}

// ignoring case since users found that all smaller case is used on some server
// and even if it is wrong according to spec, we rather have it work..

if (hp.getKey().equalsIgnoreCase("Content-Encoding")
&& hp.getValue().equalsIgnoreCase("gzip")) {
gZippedContent = true;
break;
}

}

if (gZippedContent) {
is = getUnZippedInputStream(
new BufferedInputStream(connection.openInputStream(),contentLength));
} else {
is = new BufferedInputStream(connection.openInputStream(),contentLength);
}

System.gc();

if(isFileWrite){
strOutput="CashDldLog" + intSegId + ".txt";
File fileExits = new File(Environment.getExternalStorageDirectory()+"/" + strOutput);

if(fileExits.exists())
fileExits.delete();

if (debug) {
FileOutputStream bos = new FileOutputStream(
Environment.getExternalStorageDirectory()+"/CashDldLog" + intSegId + ".txt");

byte[] bufnew = new byte[256];
while (true) {
int rd = is.read(bufnew, 0, 256);

if (rd == -1) {
break;
}

bos.write(bufnew, 0, rd);
}
bos.flush();
bos.close();
is.close();
}
}else{

parseResponse(envelope, is);
SoapPrimitive readString = (SoapPrimitive)((SoapSerializationEnvelope) envelope).getResponse();
if(readString!=null)
strOutput = readString.toString();

is.close();
}
// release all resources
// input stream is will be released inside parseResponse
os = null;
buf = null;
return strOutput;
}
catch(Exception ex)
{
throw ex;
}

}

private InputStream getUnZippedInputStream(InputStream inputStream) throws IOException {
/* workaround for Android 2.3
(see http://stackoverflow.com/questions/5131016/)
*/
try {
return (GZIPInputStream) inputStream;
} catch (ClassCastException e) {
return new GZIPInputStream(inputStream);
}
}

public ServiceConnection getServiceConnection() throws IOException {
if (serviceConnection == null) {
serviceConnection = new ServiceConnectionSE(url, 100000);
}
return serviceConnection;
}

public String getHost() {

String retVal = null;

try {
retVal = new URL(url).getHost();
} catch (MalformedURLException e) {
e.printStackTrace();
}

return retVal;
}

public int getPort() {

int retVal = -1;

try {
retVal = new URL(url).getPort();
} catch (MalformedURLException e) {
e.printStackTrace();
}

return retVal;
}

public String getPath() {

String retVal = null;

try {
retVal = new URL(url).getPath();
} catch (MalformedURLException e) {
e.printStackTrace();
}

return retVal;
}
}
Posted
Updated 11-Dec-14 20:42pm
v2

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900