package cs.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class StreamTools {
public static byte[] read(InputStream inStream) throws IOException{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte [] buffer = new byte[1024];
int len = 0;
while((len=inStream.read(buffer))!=-1){
outStream.write(buffer);
}
inStream.close();
outStream.close();
return outStream.toByteArray();
}
}
package cs.service;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Xml;
import cs.utils.StreamTools;
public class CityService {
public String query(String num) throws IOException, XmlPullParserException{
InputStream in = CityService.class.getClassLoader().getResourceAsStream("template.xml");
String data = new String(StreamTools.read(in),"UTF-8");
//System.out.println("length_________________________________________________________"+data.length());
//System.out.println(data);
in.close();
data = data.replace("city", num);
URL url = new URL("http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3000);
conn.setRequestMethod("POST");
conn.setRequestProperty("HOST", "webservice.webxml.com.cn");
conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
conn.setRequestProperty("Content-Length", data.getBytes().length+"");
conn.setDoOutput(true);
conn.getOutputStream().write(data.getBytes());
int code = conn.getResponseCode();
//System.out.println("code_______________________________"+code);
if(code == 200){
/*InputStreamReader isr = new InputStreamReader(conn.getInputStream(),"UTF-8");
String content = "";
int i;
while((i=isr.read())!=-1){
content+=(char)i;
}
return content;*/
return paresAddaress(conn.getInputStream());
}
return null;
}
private String paresAddaress(InputStream inputStream) throws XmlPullParserException, IOException {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(inputStream,"UTF-8");
int type = parser.getEventType();
StringBuffer sb = new StringBuffer();
while(type!=XmlPullParser.END_TAG){
if(type==XmlPullParser.START_TAG){
if("string".equals(parser.getName())){
sb.append(parser.nextText());
}
}
type = parser.next();
}
return sb.toString();
}
}
package com.example.weather;
import java.io.IOException;
import org.xmlpull.v1.XmlPullParserException;
import cs.service.CityService;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private EditText city;
private TextView show;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(android.os.Build.VERSION.SDK_INT>9){
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
city = (EditText) this.findViewById(R.id.city);
show = (TextView) this.findViewById(R.id.show);
}
public void on(View v) throws IOException, XmlPullParserException{
switch (v.getId()) {
case R.id.ok:
String cityaddress = city.getText().toString().trim();
CityService cs = new CityService();
String result = cs.query(cityaddress);
show.setText(result);
break;
default:
break;
}
}
}
<?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>
<getWeather xmlns="http://WebXml.com.cn/">
<theCityCode>city</theCityCode>
<theUserID></theUserID>
</getWeather>
</soap12:Body>
</soap12:Envelope>
网盘保存地址:链接: http://pan.baidu.com/s/1ntqe8rB