public String SQLInsert(String Conn, String Query)
{
SoapObject request = new
SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
PropertyInfo pi = new PropertyInfo();
pi.setName("Conn");
pi.setValue(Conn);
pi.setType(String.class);
request.addProperty(pi);
pi = new PropertyInfo();
pi.setName("Query");
pi.setValue(Query);
pi.setType(String.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
Object response = null;
try {
httpTransport.call(SOAP_ACTION,envelope);
response = envelope.getResponse();
}catch(Exception exception){
response = exception.toString();
}
return response.toString();
}
response = envelope.getResponse();// error occurs at this line.
有时它曾经显示java.net.SockettimeoutException
,现在它显示android.os.NetworkOnMainThreadException
。我想要一个好的解决方案,从长远来看可以帮助我。
最佳答案
现在它显示android.os.NetworkOnMainThreadException
实际上,只有当您在主线程中执行网络相关代码时才会出现上述异常,因此要解决此问题,您必须使用 Thread
或 AsyncTask
。
Thread t=new Thread(){
public void run(){
SQLInsert(Conn,Query);
}
}
因此,如果您想执行 SQLInsert(-,-) 方法,只需按如下方式启动Thread
t.start();
希望这对您有帮助。
关于java - 尝试使用 WebService 从 Android 应用程序将数据插入 SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31086116/