我需要连接到webservice,这需要授权。 我试图使用标题:
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};
我收到错误: 07-25 14:03:20.922:WARN / System.err(584):org.xmlpull.v1.XmlPullParserException:expect:START_TAG {http://schemas.xmlsoap.org/soap/envelope/} Envelope(位置:START_TAG @ 1:6 in java.io. InputStreamReader @ 44f632f8)
据我所知,webservice正在返回一些HTML格式的错误,但我怎么能看到它是什么?我可以制作这样一个请求的textview.settext()或将其打印到LogCat吗?我怎么能做到,为什么我收到它?:(
然后,我尝试使用HttpTransportBasicAuth类 - 面临很多问题让它工作 - 我不得不将它添加到项目中,手动将其扩展名从HttpTransport更改为HttpTransportSE,从ServiceConnectionMIDP更改为ServiceConnectionSE,因为没有这样的ksoap2-android-assembly-2.5.7中的类。最后编译时没有错误:
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());
}
}
} 现在可以使用HttpTransportBasicAuth.call方法,因为没有错误,但我仍然收到错误: 07-25 14:03:20.922:WARN / System.err(584):org.xmlpull.v1.XmlPullParserException:expect:START_TAG {http://schemas.xmlsoap.org/soap/envelope/} Envelope(位置:START_TAG @ 1:6 in java.io. InputStreamReader @ 44f632f8)当我运行项目时。 这是我尝试连接到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();
}
最后,我尝试使用此来源:
Webservice Http Authentication - KSOAP2 on Android
但是编译器不知道HeaderProperty是什么。我应该导入什么呢?
请回答我如何才能看到webservice在<html>
标签中返回的确切错误消息,因为我在LogCat中收到错误,如果我在尝试使其工作时出错,请回答。< / p>
答案 0 :(得分:1)
您需要使用ksoap2-Android 2.5.7(http://code.google.com/p/ksoap2-android/wiki/ProjectNews)
在那里你可以找到HeaderProperty类。你展示的链接至少对我有用。