如何在Android中调用Web服务实现登录系统?

时间:2015-08-03 04:31:58

标签: web-services http soap android-asynctask android-ksoap2

我有一个xml项目。这是Login示例代码

http://10.99.99.99:8087/item/services/ItemService

   <soapenv:Header/>
   <soapenv:Body>
      <ser:userLogin>
         <!--Optional:-->
         <arg0>admin</arg0>
         <!--Optional:-->
         <arg1>admin</arg1>
      </ser:userLogin>
   </soapenv:Body>
</soapenv:Envelope>

如果成功

<soap:Body>
      <ns2:userLoginResponse xmlns:ns2="http://services.ws.item/">
         <return>
            <result>1</result>
            <msg>Login Succeed</msg>
         </return>
      </ns2:userLoginResponse>
   </soap:Body>
</soap:Envelope>

如果不成功

   <soap:Body>
      <ns2:userLoginResponse xmlns:ns2="http://services.ws.item/">
         <return>
            <result>-1</result>
            <msg>Login failed</msg>
         </return>
      </ns2:userLoginResponse>
   </soap:Body>
</soap:Envelope>

该服务将连接到MySQL服务器。

我改变了测试代码,但它仍然无法运行,我很确定wsdl是可行的。 logcat显示了很多错误。

    public class CheckLoginActivity extends Activity {


        private static final String SOAP_ACTION = "http://services.ws.item/userLogin";
        private static final String METHOD_NAME = "userLogin";
        private static final String NAMESPACE = "http://services.ws.item/";
        private static final String URL = "http://10.99.99.99:8087/item/services/ItemService?wsdl";
        private TextView tv;
        private String response;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            tv= (TextView)findViewById(R.id.tv1);

            myAsyncTask myRequest = new myAsyncTask();
            myRequest.execute();

        }

        private class myAsyncTask extends AsyncTask<Void, Void, Void>    {


            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
//              tv.setText(response);
            }

            @Override
            protected void onPreExecute() {
                super.onPreExecute();       
            }

            @Override
            protected Void doInBackground(Void... arg0) {

                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                request.addProperty("arg0", "admin"); 
                request.addProperty("arg1", "admin"); 

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

                HttpTransportSE httpTransport = new HttpTransportSE(URL);

                httpTransport.debug = true;  
                try {
                    httpTransport.call(SOAP_ACTION, envelope);
                } catch (HttpResponseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (XmlPullParserException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } //send request
                SoapPrimitive result;
                try {
                    result = (SoapPrimitive) envelope.getResponse();
                    Log.d("App",""+result.toString());
                    response = result.toString();
                } catch (SoapFault e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                return null;
            }   
        }   
    }

0 个答案:

没有答案