使用KSOAP2插入数据

时间:2015-08-15 01:18:14

标签: android web-services ksoap2

单击“保存”按钮后,我无法将数据发送到数据库表中。我已将soapEnvelope从Ver11更改为Ver12。以下是我的代码:

private static String NAMESPACE = "http://10.64.13.22/AWS/";   
private static String URL = "http://10.64.13.22/AWS/Service.asmx";
private static String METHOD_NAME = "AddStaff";    
String SOAP_ACTION = "http://10.64.13.22/AWS/AddStaff";

Button save = (Button) findViewById(R.id.savebtn);
save.setOnClickListener(new OnClickListener(){          
        @Override
        public void onClick(View v) {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

            pid = tv.getText().toString();
            first_Name = fname.getText().toString();
            last_Name = lname.getText().toString();

            PropertyInfo idProp =new PropertyInfo();
            idProp.setName("PhoneId");
            idProp.setValue(pid);
            idProp.setType(Integer.class);
            request.addProperty(idProp);

            //Pass value for firstName variable of the web service
            PropertyInfo prop =new PropertyInfo();
            prop.setName("FirstName");
            prop.setValue(first_Name);
            prop.setType(Integer.class);
            request.addProperty(prop);

            PropertyInfo lnameProp =new PropertyInfo();
            lnameProp.setName("LastName");
            lnameProp.setValue(last_Name);
            lnameProp.setType(String.class);
            request.addProperty(lnameProp);


            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
            envelope.setOutputSoapObject(request);


            try {
                  HttpTransportSE httpTransport = new HttpTransportSE(URL);  
                  httpTransport.call(SOAP_ACTION, envelope);    
                  SoapObject result = (SoapObject)envelope.bodyIn; 

                if(result != null)
                {
                    //Intent next = new Intent (getApplicationContext(), MainActivity.class);
                    //startActivity(next);
                }
                else
                {
                      Toast.makeText(getApplicationContext(), "ERROR",Toast.LENGTH_LONG).show();
                }
            } catch (Exception e) {
                e.printStackTrace();
                }
        }            
    }); 
POST /AWS/Service.asmx HTTP/1.1
Host: 10.64.13.22
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/AddStaff"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <AddStaff xmlns="http://tempuri.org/">
      <PhoneId>string</PhoneId>
      <FirstName>string</FirstName>
      <LastName>string</LastName>
      <MobileTel>string</MobileTel>
      <Gender>string</Gender>
      <Email>string</Email>
      <Password>string</Password>
    </AddStaff>
  </soap:Body>
</soap:Envelope>

以下是我的LogCat:

08-14 18:13:57.871: W/System.err(10219): android.os.NetworkOnMainThreadException
08-14 18:13:57.901: W/System.err(10219):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1166)
08-14 18:13:57.901: W/System.err(10219):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
08-14 18:13:57.901: W/System.err(10219):    at libcore.io.IoBridge.connectErrno(IoBridge.java:144)
08-14 18:13:57.901: W/System.err(10219):    at libcore.io.IoBridge.connect(IoBridge.java:112)
08-14 18:13:57.911: W/System.err(10219):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
08-14 18:13:57.911: W/System.err(10219):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
08-14 18:13:57.911: W/System.err(10219):    at java.net.Socket.connect(Socket.java:843)
08-14 18:13:57.911: W/System.err(10219):    at com.android.okhttp.internal.Platform.connectSocket(Platform.java:131)
08-14 18:13:57.911: W/System.err(10219):    at com.android.okhttp.Connection.connect(Connection.java:101)
08-14 18:13:57.911: W/System.err(10219):    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
08-14 18:13:57.911: W/System.err(10219):    at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
08-14 18:13:57.911: W/System.err(10219):    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
08-14 18:13:57.911: W/System.err(10219):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
08-14 18:13:57.911: W/System.err(10219):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
08-14 18:13:57.911: W/System.err(10219):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
08-14 18:13:57.921: W/System.err(10219):    at org.ksoap2.transport.ServiceConnectionSE.openOutputStream(ServiceConnectionSE.java:126)
08-14 18:13:57.921: W/System.err(10219):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:179)
08-14 18:13:57.921: W/System.err(10219):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:116)
08-14 18:13:57.921: W/System.err(10219):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:111)
08-14 18:13:57.921: W/System.err(10219):    at com.nfc.attendance.SignUpActivity$2.onClick(SignUpActivity.java:166)
08-14 18:13:57.921: W/System.err(10219):    at android.view.View.performClick(View.java:4640)
08-14 18:13:57.921: W/System.err(10219):    at android.view.View$PerformClick.run(View.java:19431)
08-14 18:13:57.921: W/System.err(10219):    at android.os.Handler.handleCallback(Handler.java:733)
08-14 18:13:57.921: W/System.err(10219):    at android.os.Handler.dispatchMessage(Handler.java:95)
08-14 18:13:57.921: W/System.err(10219):    at android.os.Looper.loop(Looper.java:146)
08-14 18:13:57.921: W/System.err(10219):    at android.app.ActivityThread.main(ActivityThread.java:5598)
08-14 18:13:57.921: W/System.err(10219):    at java.lang.reflect.Method.invokeNative(Native Method)
08-14 18:13:57.931: W/System.err(10219):    at java.lang.reflect.Method.invoke(Method.java:515)
08-14 18:13:57.931: W/System.err(10219):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
08-14 18:13:57.931: W/System.err(10219):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
08-14 18:13:57.931: W/System.err(10219):    at dalvik.system.NativeStart.main(Native Method)  

2 个答案:

答案 0 :(得分:0)

请参阅:从最简单的Android应用程序调用ASP.NET Webservice(ASMX)

http://www.codeproject.com/Articles/304302/Calling-Asp-Net-Webservice-ASMX-From-an-Android-Ap

答案 1 :(得分:0)

您无法在主线程上运行网络

使用asynctask来运行soap请求