java - 尝试使用 WebService 从 Android 应用程序将数据插入 SQL Server

标签 java android sql multithreading web-services

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

实际上,只有当您在主线程中执行网络相关代码时才会出现上述异常,因此要解决此问题,您必须使用 ThreadAsyncTask

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/

相关文章:

java.sql.SQLException :[Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application

java - 如何从 varnish 缓存中获取正确的 .xls 文件?

android - Jitpack doent 构建项目

java - 合并排序 IllegalArgumentException

java - 无法使用 bndtools 插件运行简单的 osgi 应用程序

首选项中未设置 Android SDK

android - 使用 AlarmManager/BroadcastReceiver 在 Android 中每周一重复闹钟

mysql - SQL - 在 MySQL 中进行聚合

MySQL查询以最少的数据量获取行?

python - Mysql似乎无法识别Python中的WHERE语句