您能否向我提供一个示例,说明如何从Android中的Web服务读取chuncked响应
感谢
修改 我试着打电话给一个肥皂网服务,用一个代表图像的base64编码字符串回复我
这是代码:
String SOAP_ACTION = "service soap action";
try {
URL u = new URL("server url");
URLConnection uc = u.openConnection();
HttpURLConnection connection = (HttpURLConnection) uc;
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("SOAPAction", SOAP_ACTION);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-type", "text/xml; charset=utf-8");
String xmldata="soap request envelope";
//send the request
OutputStream out = connection.getOutputStream();
Writer wout = new OutputStreamWriter(out);
wout.write(xmldata);
wout.flush();
wout.close();
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String result;
StringBuilder builder=new StringBuilder();
//read response
while ((result=rd.readLine()) != null) {
builder.append(result);
}
答案 0 :(得分:1)
提问者注意:在Preachy答案中留下道歉。我相信你已经考虑过那些选择。但它将帮助那些能够使用SOAP库的人。如果您因特定原因决定不使用SOAP库,请将其放在评论中以获取其他人的利益。
答案 1 :(得分:0)
尝试使用更通用的类,例如DefaultHttpClient和HttpPost来处理更高级别的HTTP交互。
答案 2 :(得分:0)
您不想使用urlconnection类。你会想要使用与android捆绑在一起的httpclient,如下所示。
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://something.com/something");
HttpResponse response = client.execute();
int code = response.getStatusLine().getStatusCode();
if(code == 200){
InputStream is = response.getEntity().getContent();
//Now parse the xml coming back
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
YourParser parser = new YourParser();
xr.setContentHandler(parser);
xr.parse(new InputSource(is));
}
您应该只需要创建xml解析器来解析对象。希望这可以帮助。