Java 尝试使用WebService从Android应用程序在SQL server中插入数据
有时它会显示Java 尝试使用WebService从Android应用程序在SQL server中插入数据,java,android,sql,multithreading,web-services,Java,Android,Sql,Multithreading,Web Services,有时它会显示java.net.SockettimeoutException,现在它显示android.os.NetworkOnMainThreadException。我想要一个能长期帮助我的好解决方案。现在它显示android.os.NetworkOnMainThreadException 实际上,只有在主线程中执行与网络相关的代码时,才会出现上述异常,因此要解决此问题,必须使用线程或异步任务 public String SQLInsert(String Conn, String Query)
java.net.SockettimeoutException
,现在它显示android.os.NetworkOnMainThreadException
。我想要一个能长期帮助我的好解决方案。现在它显示android.os.NetworkOnMainThreadException
实际上,只有在主线程中执行与网络相关的代码时,才会出现上述异常,因此要解决此问题,必须使用线程
或异步任务
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.
因此,如果要执行SQLInsert(--)方法,只需按如下所示启动线程
Thread t=new Thread(){
public void run(){
SQLInsert(Conn,Query);
}
}
希望这能对您有所帮助。现在显示的是android.os.NetworkOnMainThreadException
实际上,只有在主线程中执行与网络相关的代码时,才会出现上述异常,因此要解决此问题,必须使用线程
或异步任务
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.
因此,如果要执行SQLInsert(--)方法,只需按如下所示启动线程
Thread t=new Thread(){
public void run(){
SQLInsert(Conn,Query);
}
}
希望这对你有帮助