2

I need to connect to webservice, which requers authorization. I tried to use headers:

Element usernameElement = new Element().createElement(NAMESPACE, "Username");
                       usernameElement.addChild(Node.TEXT, "user");
                       Element passwordElement = new Element().createElement(NAMESPACE,   "Password");
                        passwordElement.addChild(Node.TEXT, "pass");
                        Element header = new Element().createElement(NAMESPACE, "AuthHeader");
                        header.addChild(Node.ELEMENT, usernameElement);
                        header.addChild(Node.ELEMENT, passwordElement);

soapEnvelope.headerOut= new Element[]{header};

I receive the error: 07-25 14:03:20.922: WARN/System.err(584): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG @1:6 in java.io.InputStreamReader@44f632f8)

I understand, webservice is returning some HTML formatted error, but how can i see what is it? Can i make textview.settext() of such a request or print it to LogCat? How can i make it and Why am i receiving it?:(

Then, i tried to use HttpTransportBasicAuth class - faced a lot of problems to make it work - i had to add it to the project, change manually its extension from HttpTransport to HttpTransportSE and from ServiceConnectionMIDP to ServiceConnectionSE because there wasn't such classes in ksoap2-android-assembly-2.5.7. At last it compiled without errors:

package com.android.testinet;

import org.ksoap2.transport.*;

import org.ksoap2.transport.HttpTransportSE;
import java.io.*;  

public class HttpTransportBasicAuth extends HttpTransportSE {
     private String username;   
        private String password;   
        /**  
         * Constructor with username and password  
         *   
         * @param url  
         *            The url address of the webservice endpoint  
         * @param username  
         *            Username for the Basic Authentication challenge RFC 2617   
         * @param password  
         *            Password for the Basic Authentication challenge RFC 2617  
         */   
        public HttpTransportBasicAuth(String url, String username, String password) {   
        super(url);   
        this.username = username;   
        this.password = password;   
    }   

    protected ServiceConnection getServiceConnection() throws IOException {   
        ServiceConnectionSE midpConnection = new ServiceConnectionSE(url);   
        addBasicAuthentication(midpConnection);   
        return midpConnection;   
    }   

    protected void addBasicAuthentication(ServiceConnection midpConnection) throws IOException {   
        if (username != null && password != null) {   
            StringBuffer buf = new StringBuffer(username);   
            buf.append(':').append(password);   
            byte[] raw = buf.toString().getBytes();   
            buf.setLength(0);   
            buf.append("Basic ");   
            org.kobjects.base64.Base64.encode(raw, 0, raw.length, buf);   
            midpConnection.setRequestProperty("Authorization", buf.toString());   
        }   
    }   

} it is ok now with the HttpTransportBasicAuth.call method, as there is no an error, but i'm still receiving the error: 07-25 14:03:20.922: WARN/System.err(584): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG @1:6 in java.io.InputStreamReader@44f632f8) when i run project. Here's my code by which i try to connect to webservice:

  HttpTransportBasicAuth aht= new HttpTransportBasicAuth(URL, "user", "pass");
          try
          {

            aht.call(SOAP_ACTION, soapEnvelope);

            SoapPrimitive tmp_ResultString = (SoapPrimitive) soapEnvelope.getResponse();
            ResultString = tmp_ResultString.toString();
         // tv.setText(ResultString);
          }
          catch(Exception e)
          {
            e.printStackTrace();

          }

And finally, i tried to use this source: Webservice Http Authentication - KSOAP2 on Android but compiler do not know what HeaderProperty is. What should i import to be it ok? Please answer how can i see what the exact error message webservice returns in <html> tag, because of what i'm receiving the error in LogCat and please answer if i'm doing anything wrong while trying to make it work.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Keem
  • 21
  • 1
  • 3

1 Answers1

1

You need to use ksoap2-Android 2.5.7 (http://code.google.com/p/ksoap2-android/wiki/ProjectNews)

There you can find the class HeaderProperty. The link you show works for me at least.

idiaz
  • 21
  • 1
  • 6