Android_xmlアップロード
4007 ワード
1.servlet
public class XmlServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
byte[] data=StreamTool.read(request.getInputStream());
String xml=new String(data,"UTF-8");
System.out.println(xml);
}
}
2.Test
// XML webService
public class XMLTest extends AndroidTestCase {
public void testSendXml() throws Exception{
InputStream input=this.getClass().getClassLoader().getResourceAsStream("xml/person.xml");
byte[] data=StreamTool.read(input);
// String xml=new String(data);
String path="http://192.168.1.17:8080/Test1/xmlServlet";
HttpURLConnection con=(HttpURLConnection) new URL(path).openConnection();
con.setConnectTimeout(5000);
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setRequestProperty("Content-Type","text/xml;charset=utf-8");
con.setRequestProperty("Content-Length",String.valueOf(data.length));
con.getOutputStream().write(data);
if(con.getResponseCode()==200){
Log.i("post","xml ");
}else{
Log.i("post","xml ");
}
}
}
3. webservice http://www.webxml.com.cn/zh_cn/index.aspx
4.phoneSoap.xml
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<getMobileCodeInfo xmlns="http://WebXml.com.cn/">
<mobileCode>$phone</mobileCode>
<userID></userID>
</getMobileCodeInfo>
</soap12:Body>
</soap12:Envelope>
5.service
public class PhoneSaopService {
/** , webService*/
public static String getAddress(String phone) throws Exception{
String soap=readSoap();
soap=soap.replace("$phone", phone);
Log.i("webService",soap);
byte[] entity=soap.getBytes();
//API
String path="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
HttpURLConnection con=(HttpURLConnection) new URL(path).openConnection();
con.setConnectTimeout(5000);
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setRequestProperty("Content-Type","application/soap+xml; charset=utf-8");
con.setRequestProperty("Content-Length",String.valueOf(entity.length));
con.getOutputStream().write(entity);
if(con.getResponseCode()==200){
return parseSoap(con.getInputStream());
}
return null;
}
/**
* soap
*/
private static String parseSoap(InputStream xmlStream) throws Exception {
XmlPullParser parser=Xml.newPullParser();
parser.setInput(xmlStream,"UTF-8");
int event=parser.getEventType();
while(event!=parser.END_DOCUMENT){
switch (event){
case XmlPullParser.START_TAG:
if("getMobileCodeInfoResult".equals(parser.getName())){
return parser.nextText();
}
break;
}
event=parser.next();
}
return null;
}
/** soap */
private static String readSoap() throws Exception {
InputStream inputStream=PhoneSaopService.class.getClassLoader()
.getResourceAsStream("xml/phoneSoap.xml");
byte[] data=StreamTool.read(inputStream);
return new String(data);
}
}