Authentication Fails using ksoap2 Android

六月ゝ 毕业季﹏ 提交于 2019-12-11 17:04:54

问题


Hi I am using ksoap2 for data parsing in Android. I am not able to understand exact issue with my code. I need to parse data in ksoap2. can any one help me to solve this issue. whenever I tried to parse, in my response it is giving 'Authentication Fail', I have already checked that , in my header I am passing username and password properly. Following is my code please let me know any solution

This is the format for soap api.

POST /Wt/webtask.asmx HTTP/1.1
Host: www.myweb.co.ke
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/AddItem"

<?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:Header>
    <UserCredential xmlns="http://tempuri.org/">
      <userName>string</userName>
      <password>string</password>
    </UserCredential>
  </soap:Header>
  <soap:Body>
    <AddItem xmlns="http://tempuri.org/">
      <UserName>string</UserName>
      <ItemCode>string</ItemCode>
      <Description>string</Description>
      <Packaging>string</Packaging>
      <Unit>string</Unit>
      <Photo>string</Photo>
    </AddItem>
  </soap:Body>
</soap:Envelope>

Java Code

private static final String METHOD_NAME = "AddItem";
    private static final String NAMESPACE = "https://www.myweb.co.ke/Wt/";
    private static final String SOAP_ACTION = "http://tempuri.org/AddItem";
    private static final String URL = "https://www.aspiresoft.co.ke/Wt/webtask.asmx";
    private String resultString;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_demo);

        Button btnClick = (Button) findViewById(R.id.bt);
        btnClick.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                SOAP_WebService.AsyncCallWS task = new SOAP_WebService.AsyncCallWS();
                task.execute();
            }
        });
    }



    public void soapAddItem() {
        try {


            //Prepare the header with the authentication data.
            Element headers = new Element().createElement(SOAP_WebService.NAMESPACE, "UserCredential");

            Element username = new Element().createElement(SOAP_WebService.NAMESPACE, "userName");
            username.addChild(Node.TEXT, "1234");
            headers.addChild(Node.ELEMENT, username);

            Element pass = new Element().createElement(SOAP_WebService.NAMESPACE, "password");
            pass.addChild(Node.TEXT, "4321");
            headers.addChild(Node.ELEMENT, pass);

            // Soap Request
            final SoapObject request = new SoapObject(SOAP_WebService.NAMESPACE, SOAP_WebService.METHOD_NAME);
                     /*   request.addProperty("UserName", "John");
                        request.addProperty("ItemCode", "102120");
                        request.addProperty("Description", "FLOUR EXE 2kg");
                        request.addProperty("Packaging", "1ct x 12pc");
                        request.addProperty("Unit", "PC");
                        request.addProperty("Photo", "aksdjflksjdlfksjdl99389-sdfd=/kdkflsk");*/

            SoapObject avsdf = new SoapObject(SOAP_WebService.NAMESPACE, "UserName");
            avsdf.setInnerText("John");
            request.addSoapObject(avsdf);

            avsdf = new SoapObject(SOAP_WebService.NAMESPACE, "ItemCode");
            avsdf.setInnerText("102120");
            request.addSoapObject(avsdf);

            avsdf = new SoapObject(SOAP_WebService.NAMESPACE, "Description");
            avsdf.setInnerText("FLOUR EXE 2kg");
            request.addSoapObject(avsdf);

            avsdf = new SoapObject(SOAP_WebService.NAMESPACE, "Unit");
            avsdf.setInnerText("PC");
            request.addSoapObject(avsdf);

            avsdf = new SoapObject(SOAP_WebService.NAMESPACE, "Packaging");
            avsdf.setInnerText("1ct x 12pc");
            request.addSoapObject(avsdf);

            avsdf = new SoapObject(SOAP_WebService.NAMESPACE, "Photo");
            avsdf.setInnerText("aksdjflksjdlfksjdl99389-sdfd=/kdkflsk");
            request.addSoapObject(avsdf);

            final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            // add header to envelope
            envelope.headerOut = new Element[1];
            envelope.headerOut[0] = headers;
            envelope.setOutputSoapObject(request);

            //Add the header to the envelope.
            final HttpTransportSE androidHttpTransport = new HttpTransportSE(SOAP_WebService.URL);
            androidHttpTransport.debug = true;
            androidHttpTransport.call(SOAP_WebService.SOAP_ACTION, envelope);
            Log.d("Login Request", "Request:- " + androidHttpTransport.requestDump);

            final SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
            Log.d("Login Response", "Response:- " + androidHttpTransport.responseDump);
            // final SoapObject result = (SoapObject) envelope.getResponse();
            String response = result.toString();
            resultString = result.toString();
            Log.d("Login Responsed", "Responsed:- " + response);
        } catch (Exception ex) {
            Log.e("Login", "soapLogin:- " + ex.getMessage());
        }

    }

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

        @Override
        protected void onPreExecute() {
            Log.i("", "onPreExecute");
        }

        @Override
        protected Void doInBackground(Void... params) {
            Log.i("", "doInBackground");
            soapAddItem();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            Log.i("", "onPostExecute");
            Toast.makeText(SOAP_WebService.this, "Response" + resultString.toString(), Toast.LENGTH_LONG).show();
        }

    }

来源:https://stackoverflow.com/questions/56877369/authentication-fails-using-ksoap2-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!