android访问netbeans开发的java webservice

wula0010 2014-02-27 05:13:25
用netbeans开发的webservice服务,地址:http://localhost:8082/WebService/MyTestService?tester,可以看到测试界面,输入参数后正常返回,并看到soap请求和响应的xml格式:

SOAP 请求

--------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Header/>
<S:Body>
<ns2:add xmlns:ns2="http://service/">
<num1>3</num1>
<num2>4</num2>
</ns2:add>
</S:Body>
</S:Envelope>


--------------------------------------------------------------------------------

SOAP 响应

--------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:addResponse xmlns:ns2="http://service/">
<return>7</return>
</ns2:addResponse>
</S:Body>
</S:Envelope>


我在android程序里:

InputStream inStream;
inStream = getAssets().open("test.xml");
String soap = readSoapFile(inStream, num1,num2);
byte[] data = soap.getBytes();
// 提交Post请求
URL url = new URL("http://10.93.136.5:8082/WebService/MyTestService");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5 * 1000);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
if (conn.getResponseCode() == 200)
{
// 解析返回信息
return parseResponseXML(conn.getInputStream());
}

private String readSoapFile(InputStream inStream, String num1, String num2) throws Exception
{
// 从流中获取文件信息
byte[] data = readInputStream(inStream);
String soapxml = new String(data);
// 占位符参数
Map<String, String> params = new HashMap<String, String>();
params.put("param1", num1);
params.put("param2", num2);
// 替换文件中占位符
return replace(soapxml, params);
}
private byte[] readInputStream(InputStream inputStream) throws Exception
{
byte[] buffer = new byte[1024];
int len = -1;
ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1)
{
outSteam.write(buffer, 0, len);
}
outSteam.close();
inputStream.close();
return outSteam.toByteArray();
}

private String replace(String xml, Map<String, String> params) throws Exception
{
String result = xml;
if (params != null && !params.isEmpty())
{
for (Map.Entry<String, String> entry : params.entrySet())
{
String name = "\\$" + entry.getKey();
Pattern pattern = Pattern.compile(name);
Matcher matcher = pattern.matcher(result);
if (matcher.find())
{
result = matcher.replaceAll(entry.getValue());
}
}
}
return result;
}



其中test.xml为前面的SOAP 请求修改后的:

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Header/>
<S:Body>
<ns2:add xmlns:ns2="http://service/">
<num1>$param1</num1>
<num2>$param2</num2>
</ns2:add>
</S:Body>
</S:Envelope>


程序执行到:
if (conn.getResponseCode() == 200)
返回 是415,不是200,请哪位有经验的老大,看看是哪里不对?
...全文
315 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wula0010 2014-06-06
  • 打赏
  • 举报
回复
我不是说了么?不用webservice了,用servlet,android用请求web页面的方法,然后解析得到的页面。或者用.net开发webservice,这样的连接例子网上很多.....
cainingyouyou 2014-04-03
  • 打赏
  • 举报
回复
请求解决方法
wula0010 2014-03-21
  • 打赏
  • 举报
回复
不用web服务了,原来以为web服务通用,不需要管什么系统和架构,现在改用url连接,解析返回的页面了,简单好用
teemai 2014-02-27
  • 打赏
  • 举报
回复
换成get请求试试
teemai 2014-02-27
  • 打赏
  • 举报
回复
您的 Web 服务器认为,客户端(如您的浏览器或我们的 CheckUpDown 机器人)发送的 HTTP 数据流所确定的网址资源的媒体类型 1) 和请求中指定的媒体类型不一致,或 2) 与该资源的当前数据不兼容, 或 3) 和请求中指定的 HTTP 方法不兼容。
wula0010 2014-02-27
  • 打赏
  • 举报
回复
引用 1 楼 u012535657 的回复:
一般网页返回415的话,是表明请求的格式不受请求页面的支持,要不你换一种格式试试。
怎么换呢?改哪里呢???????
marlenna 2014-02-27
  • 打赏
  • 举报
回复
一般网页返回415的话,是表明请求的格式不受请求页面的支持,要不你换一种格式试试。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧