使用Ksoap进行Xmlparsing

时间:2011-02-08 11:11:59

标签: java xml java-me ksoap

我正在使用kso​​ap2进行xmlparsing。我在我的库中添加了ksoap2.jar文件。并完成了这个编码

import java.io.IOException;
import java.util.Vector;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransport;


public class KsoapMidlet extends MIDlet {

Display display;
String strUserName= "Hello";
String strPassword = "World";
long ParentID;
String ChildIMEINumber;
String ChildName;
String serviceUrl = "http://www.semaphore.co.in/ChildTrackerService/ChildTrackerService.asmx";
String serviceNameSpace = "http://tempuri.org/";
String soapAction ="http://tempuri.org/AddChild";
String methodName = "AddChild";
Vector v=new Vector();



public void startApp() {
display = Display.getDisplay(this);
parsing();
// v.addElement("Hello");
// v.addElement("World");
// Constants d=new Constants();
// d.callSoap(methodName,v);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
public void parsing(){
SoapObject request = new SoapObject(serviceNameSpace,methodName);
request.addProperty("strUserName", strUserName);
request.addProperty("strPassword", strPassword);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
envelope.bodyOut = request;
envelope.encodingStyle = SoapSerializationEnvelope.ENC;
HttpTransport transport = new HttpTransport("http://www.semaphore.co.in/ChildTrackerService/ChildTrackerService.asmx");
transport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
transport.debug = true;
String result = null;

try {
transport.call("http://tempuri.org/" + methodName, envelope);
result = (envelope.getResponse()).toString();
System.out.println("***********************result********************************");
System.out.println("result=="+result);
System.out.println("Result :" + result.toString());
}
catch (org.xmlpull.v1.XmlPullParserException ex2) {
System.out.println("XmlPullParserException :" + ex2.toString());
System.out.println("Request \n" + transport.requestDump);
System.out.println("Response \n" + transport.responseDump);
} catch (SoapFault sf) {
System.out.println("SoapFault :" + sf.faultstring);
System.out.println("Request \n" + transport.requestDump);
System.out.println("Response \n" + transport.responseDump);
} catch (IOException ioe) {
System.out.println("IOException :" + ioe.toString());
System.out.println("Request \n" + transport.requestDump);
System.out.println("Response \n" + transport.responseDump);
}
}
}

但我在行----- transport.call("http://tempuri.org/" + methodName, envelope);

中获得IO异常

错误消息是: -

IOException :java.io.IOException: malformed header field <html>

请求 &LT; ?xml version="1.0" encoding="UTF-8"?><v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"><v:Header /><v:Body><AddChild xmlns="http://tempuri.org/" id="o0" c:root="1"><strUserName i:type="d:string">Hello</strUserName><strPassword i:type="d:string">World</strPassword></AddChild></v:Body></v:Envelope>

响应

null

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。我解决它的方法是更改​​手机上的配置文件设置。转到GPRS连接设置并将协议更改为HTTP(之前在WAP上)。

答案 1 :(得分:1)

我假设HTTP响应是带有自定义HTML错误页面的HTTP 404。这是我能想象如何获得网络服务答案的唯一可能性。

可能是您在transport.call(..)中的URL错误或服务器端的Web服务未激活。

我建议使用像WebScarab(Java)或Fiddler(.Net)这样的本地http代理,并将调用重定向到此本地代理。然后,您将看到服务器作为响应发送的内容。