尝试使用WebService从Android应用程序插入SQL服务器中的数据

时间:2015-06-27 07:01:47

标签: 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。我想要一个很好的解决方案,可以长期帮助我。

1 个答案:

答案 0 :(得分:0)

现在显示android.os.NetworkOnMainThreadException

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

Thread t=new Thread(){
 public void run(){
     SQLInsert(Conn,Query);
 }
 }

因此,如果要执行SQLInsert( - , - )方法,只需按以下步骤启动Thread

t.start();

希望这会对你有所帮助。