如何在android中调用soap web服务?
当我尝试在android中调用soap Web服务时如何在android中调用soap web服务?,android,web-services,soap,Android,Web Services,Soap,当我尝试在android中调用soap Web服务时 androidHttpTransport.call(SOAP_ACTION, envelope); 程序直接跳入捕获程序应该是callresult=envelope.getResponse()但我还没有回复,有什么可能的解决方案有人可以帮忙吗 try { System.out.println("Token ===sssTTTTTT " ); SoapObject request = new SoapObject(NAMES
androidHttpTransport.call(SOAP_ACTION, envelope);
程序直接跳入捕获程序应该是callresult=envelope.getResponse()代码>但我还没有回复,有什么可能的解决方案有人可以帮忙吗
try {
System.out.println("Token ===sssTTTTTT " );
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("encAppName", "dsakjsfj");
request.addProperty("sessionInfo", "sadsadsdf");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
result = envelope.getResponse();
Toast.makeText(getBaseContext(), " Result " + "\n" + result.toString(), Toast.LENGTH_SHORT).show();
System.out.println("response === " + result.toString());
} catch (Exception e) {
// txtprint.setText(e.getMessage());
}
请尝试我的工作代码。只要做必要的改变。
如果您说它将直接捕获块,则表示它正在抛出一些异常。请尝试查看这是什么将异步任务用于后台线程(请求-响应)
你的soap服务是否正常运行?如果是,那么你必须在android中调用你的Web服务thr asynctask。在catch block中你会遇到什么异常?检查它将帮助你摆脱它。希望这对你有帮助。检查下面的代码调用这个类方法thr异步任务
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class WebCommunication {
public static String SOAP_ACTION = "";
public static String METHOD_NAME = "";
public static String NAMESPACE = "http://tempuri.org/";
public static String URL = "http://192.168.1.108:8085/wsTrinfin/Service.asmx";//your webservice
public String CALLSOAP(String Data)
{
try {
String Result="";
METHOD_NAME = "ManageLicense";
SOAP_ACTION = "http://tempuri.org/ManageLicense";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Data",Data);
request.addProperty("ActivationType","Online");
request.addProperty("ValidationParameter","TABLET");
request.addProperty("SenderPhoneNumber","NA");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE aht = new HttpTransportSE(URL);
SoapPrimitive results = null;
try
{
aht.call(SOAP_ACTION, envelope);
results = (SoapPrimitive) envelope.getResponse();
}
catch (Exception e)
{
//Log.Write("Unable to connect to webservice : " +e.toString());
return "ERROR";
}
Result= results.toString();
return Result;
} catch (Exception e) {
//Log.Write("Error Occured in :"+URL+e.toString(),Log._LogLevel.NORAML);
return "ERROR";
}
}
}
您是否使用asynctask?不,我需要一个简单的响应,然后我使用asynctask ri8?如果您正在执行与网络相关的操作,您应该使用asynctask。是的,谢谢我使用了您的代码,但在第aht.call行(SOAP\U操作,信封)之后;直接抓取捕获(异常e)现在我做什么???在上面的代码中,在androidHttpTransport.call之后(SOAP_操作,信封);这条线直接跳转到catch block中有解决方案吗?@UmangHAmin:这意味着它抛出了一些异常。打印该异常(例如printStackTrace())。并尝试查看您遇到的异常情况。
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class WebCommunication {
public static String SOAP_ACTION = "";
public static String METHOD_NAME = "";
public static String NAMESPACE = "http://tempuri.org/";
public static String URL = "http://192.168.1.108:8085/wsTrinfin/Service.asmx";//your webservice
public String CALLSOAP(String Data)
{
try {
String Result="";
METHOD_NAME = "ManageLicense";
SOAP_ACTION = "http://tempuri.org/ManageLicense";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Data",Data);
request.addProperty("ActivationType","Online");
request.addProperty("ValidationParameter","TABLET");
request.addProperty("SenderPhoneNumber","NA");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE aht = new HttpTransportSE(URL);
SoapPrimitive results = null;
try
{
aht.call(SOAP_ACTION, envelope);
results = (SoapPrimitive) envelope.getResponse();
}
catch (Exception e)
{
//Log.Write("Unable to connect to webservice : " +e.toString());
return "ERROR";
}
Result= results.toString();
return Result;
} catch (Exception e) {
//Log.Write("Error Occured in :"+URL+e.toString(),Log._LogLevel.NORAML);
return "ERROR";
}
}
}
public InputStream sendPOSTRequest(String strPostURL, String strParamToPost)
{
strPostURL = strPostURL.replace(" ", "%20");
DefaultHttpClient defaultHttpClient = getHttpClient();
HttpPost httpPost = new HttpPost(strPostURL);
httpPost.addHeader("Content-Type", "text/xml; charset=utf-8");
InputStream inputStream = null;
try
{
if(strParamToPost != null)
httpPost.setEntity(new StringEntity(strParamToPost));
LogUtils.info(LogUtils.SERVICE_LOG_TAG, "**Executing requst**");
HttpResponse response = defaultHttpClient.execute(httpPost);
LogUtils.info(LogUtils.SERVICE_LOG_TAG, "##Response received##");
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
}
catch (Exception e)
{
LogUtils.error("HttpHelper", "PostData Error :"+e.toString());
}
return inputStream;
}