[英]call asmx web service from android
I have this C# working example 我有这个C#工作示例
void Main()
{
string r = String.Format("<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><TrackMobileApp xmlns=\" url \" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><device>test device</device><imei>000000</imei><ipAddress>192.168.1.1</ipAddress><timeStamp>{0}</timeStamp></TrackMobileApp></s:Body></s:Envelope>", DateTime.Now.ToString("o"));
HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create("asmx url");
wr.ProtocolVersion = HttpVersion.Version11;
wr.Headers.Add("SOAPAction", "\"soap action url\"");
wr.Method = "POST";
wr.ContentType = "text/xml;charset=utf-8";
using (StreamWriter sw = new StreamWriter(wr.GetRequestStream()))
{
sw.Write(r);
}
HttpWebResponse rs = (HttpWebResponse)wr.GetResponse();
if (rs.StatusCode == HttpStatusCode.OK)
{
XmlDocument xd = new XmlDocument();
using (StreamReader sr = new StreamReader(rs.GetResponseStream()))
{
xd.LoadXml(sr.ReadToEnd());
xd.InnerXml.Dump();
}
}
}
I've tried this in android: 我已经在android中尝试过了:
private class DownloadTask extends AsyncTask<String, Void, String> {
private String downloadContent(String myurl) throws IOException {
InputStream is = null;
int length = 500;
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestProperty("SOAPAction", "soap action url");
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "text/xml;");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
int response = conn.getResponseCode();
Log.d(TAG, "The response is: " + response);
is = conn.getInputStream();
// Convert the InputStream into a string
String contentAsString = convertInputStreamToString(is, length);
return contentAsString;
} finally {
if (is != null) {
is.close();
}
}
}
...
}
and I'm calling it using: 我用它来称呼它
new DownloadTask().execute("asmx url"); 新的DownloadTask()。execute(“ asmx url”);
how can I attach body to that HttpURLConnection
? 如何将正文附加到该
HttpURLConnection
?
or if there's something more to do tell me.. I'm beginner at java 还是有其他事情要告诉我。我是Java的初学者
Try this 尝试这个
if(body != null) {
DataOutputStream writer = new DataOutputStream(conn.getOutputStream());
byte[] data = body.toString().getBytes("UTF-8");
writer.write(data);
writer.flush();
writer.close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.