Alternative To Using Ksoap For Accessing Webservice From Android Application Due To Ksoap Jar Size
Solution 1:
This is a late answer but it seams like nobody has attempted it before. I made my own service client class, its not as generic as ksoap2 but maybe this is what you are looking for:
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.GZIPInputStream;
publicclassservice_clientextendsAsyncTask<String, Void, String>
{
privatestatic ProgressDialog dialog;
privatestaticintReadTimeout=10000;
privatestaticintConnectTimeout=10000;
privatestaticStringurlString="http://www.webservicex.net/globalweather.asmx?";
privatestaticStringnamespace="http://www.webserviceX.NET";
privatestaticStringsoapAction= namespace +"/";
private String[] cacheParams;
private Context context;
publicservice_client(Context context)
{
this.context = context;
}
@OverrideprotectedvoidonPreExecute()
{
try {
if(dialog != null && dialog.isShowing())
dialog.dismiss();
dialog = ProgressDialog.show(context, "", "Loading...", false);
}
catch (Exception ignored)
{}
}
@Overrideprotected String doInBackground(String... Params)
{
try {
cacheParams = Params;
// Create soap messageStringBuildersb=newStringBuilder();
sb.append("<v:Envelope xmlns:v=\"http://schemas.xmlsoap.org/soap/envelope/\"><v:Body><")
.append(Params[0])
.append(" xmlns=\"")
.append(namespace)
.append("\">");
for (String param : Params) {
String[] parameter_data = param.split("\\|", 2);
if (parameter_data.length == 2) {
sb.append("<")
.append(parameter_data[0])
.append(">")
.append(parameter_data[1])
.append("</")
.append(parameter_data[0])
.append(">");
}
}
sb.append("</")
.append(Params[0])
.append("></v:Body></v:Envelope>");
Stringcontent= sb.toString();
// create post for soap messageURLurl=newURL(urlString);
HttpURLConnectionconnection= (HttpURLConnection) url.openConnection(); // this needs to be HttpsURLConnection if you are using ssl
connection.setReadTimeout(ReadTimeout);
connection.setConnectTimeout(ConnectTimeout);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
connection.setRequestProperty("Content-Length", String.valueOf(content.length()));
connection.setRequestProperty("Accept-Encoding", "gzip"); // comment this line out if you dont want to compress all service calls
connection.setRequestProperty("SOAPAction", soapAction + Params[0]);
// Get soap responseOutputStreamos= connection.getOutputStream();
os.write(content.getBytes("UTF-8"));
os.flush();
os.close();
Stringresponse="";
intresponseCode= connection.getResponseCode();
BufferedReader br;
if (responseCode == HttpURLConnection.HTTP_OK) {
String line;
br = connection.getContentEncoding().equals("gzip") ?
newBufferedReader(newInputStreamReader(getUnZippedInputStream(connection.getInputStream()))) :
newBufferedReader(newInputStreamReader(connection.getInputStream()));
while ((line = br.readLine()) != null) {
response += line;
}
} else {
thrownewException("HTTP ERROR: " + responseCode);
}
// Extract dataintindex= response.indexOf(Params[0] + "Result>") + 7+Params[0].length();
response = response.substring(index, response.indexOf("</"+Params[0], index));
// release all resources
br.close();
connection.disconnect();
return response;
}
catch (Exception ex)
{
return ex.getClass().toString();
}
}
@OverrideprotectedvoidonPostExecute(String result)
{
try
{
if(result.equals("class java.net.ConnectException") || result.equals("class java.net.UnknownHostException") || result.equals("class java.net.SocketTimeoutException"))
{
//you can ask the user if they would like to retry as there was a network error//use the cacheParams to recall the method
}
}
finally
{
try {
dialog.dismiss();
}
catch (Exception ignored)
{}
}
}
//gzip compressionprivate GZIPInputStream getUnZippedInputStream(InputStream inputStream)throws IOException {
/* workaround for Android 2.3
(see http://stackoverflow.com/questions/5131016/)
*/try {
return (GZIPInputStream)inputStream;
} catch (ClassCastException e) {
returnnewGZIPInputStream(inputStream);
}
}
}
obviously this is an example soap service being used. Anyway this is how I call the service with this class:
new service_client(this).execute("GetWeather", //first param being the method name
"CityName|Cape Town", //params are seperated by a '|'characterfor simplicity
"CountryName|South Africa");
Solution 2:
You can reduce the size of the code added to only what is actually needed by using Proguard. You could also use proguard with the maven android plugin if you are building your app with maven. Also if you are using it on Android you should be using the latest release (2.5.1) of the ksoap2-android project. And yes, imho it is worth the additional size for a reasonable app talking to a few web services or if you need to get things working sooner rather than later.
Post a Comment for "Alternative To Using Ksoap For Accessing Webservice From Android Application Due To Ksoap Jar Size"