android 根据IP获取天气情况 详细讲解



android 根据IP获取天气情况 详细讲解

分析:
此功能必须在可用的网络下运行的,确保在有可用网络下才能正常运行,获取当前网络IP判断所在城市,通过城市查询天气

 

1、首先判断网络是否正常(笔者做的是平板应用的一个模块有手机有些功能不一样)

[java] view plaincopy
public void getLocalIPAddress() {

WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (!wm.isWifiEnabled()) {
Toast.makeText(this, “没有可用的网络”, Toast.LENGTH_LONG).show();
}
}

2、其次要自动获取IP地址(webservice借口、相关网址),在此,笔者是根据网址获取,在进行解析

getCityIP()

[java] view plaincopy
public void getCityIP() {
URL url;
URLConnection conn = null;
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
String str = “”;
org.jsoup.nodes.Document doc;
try {
url = new URL(“http://city.ip138.com/city.asp”);
conn = url.openConnection();
is = conn.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String input = “”;
while ((input = br.readLine()) != null) {
str += input;
}
doc = Jsoup.parse(str);
String ip1 = doc.body().text();
int start = ip1.indexOf(“[");
int end = ip1.indexOf("]“);
setIp(ip1.substring(start + 1, end));
} catch (Exception e) {
e.printStackTrace();
}

}

3、再次根据IP地址获取城市(webservice借口、解析网页)

getCityByIp():

[java] view plaincopy
public void getCityByIp() {
try {
URL url = new URL(“http://whois.pconline.com.cn/ip.jsp?ip=” + getIp());
HttpURLConnection connect = (HttpURLConnection) url
.openConnection();
InputStream is = connect.getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buff = new byte[256];
int rc = 0;
while ((rc = is.read(buff, 0, 256)) > 0) {
outStream.write(buff, 0, rc);

}
System.out.println(outStream);
byte[] b = outStream.toByteArray();

// 关闭
outStream.close();
is.close();
connect.disconnect();
String address = new String(b,”GBK”);
if (address.startsWith(“北”)||address.startsWith(“上”)||address.startsWith(“重”)){
setCity(address.substring(0,address.indexOf(“市”)));
}
if(address.startsWith(“香”)){
setCity(address.substring(0,address.indexOf(“港”)));
}
if(address.startsWith(“澳”)){
setCity(address.substring(0,address.indexOf(“门”)));
}
if (address.indexOf(“省”) != -1) {
setCity(address.substring(address.indexOf(“省”) + 1, address.indexOf(“市”)));
}
} catch (Exception e) {
e.printStackTrace();
}
}
4、进行天气查询实现webservice接口(http://www.webxml.com.cn/webservices/weatherwebservice.asmx)

[java] view plaincopy
private static final String NAMESPACE = “http://WebXml.com.cn/”;


// WebService地址
private static String URL = “http://www.webxml.com.cn/webservices/weatherwebservice.asmx”;
private static final String METHOD_NAME = “getWeatherbyCityName”;

private static String SOAP_ACTION = “http://WebXml.com.cn/getWeatherbyCityName”;
private String weatherToday;
private SoapObject detail;
private String weatherNow;
private String weatherWillBe;

private void setIcon(String weather, ImageView imageview) {
if (weather.equalsIgnoreCase(“nothing.gif”))
imageview.setBackgroundResource(R.drawable.a_nothing);
if (weather.equalsIgnoreCase(“0.gif”))
imageview.setBackgroundResource(R.drawable.a_0);
if (weather.equalsIgnoreCase(“1.gif”))
imageview.setBackgroundResource(R.drawable.a_1);
if (weather.equalsIgnoreCase(“2.gif”))
imageview.setBackgroundResource(R.drawable.a_2);
if (weather.equalsIgnoreCase(“3.gif”))
imageview.setBackgroundResource(R.drawable.a_3);
if (weather.equalsIgnoreCase(“4.gif”))
imageview.setBackgroundResource(R.drawable.a_4);
if (weather.equalsIgnoreCase(“5.gif”))
imageview.setBackgroundResource(R.drawable.a_5);
if (weather.equalsIgnoreCase(“6.gif”))
imageview.setBackgroundResource(R.drawable.a_6);
if (weather.equalsIgnoreCase(“7.gif”))
imageview.setBackgroundResource(R.drawable.a_7);
if (weather.equalsIgnoreCase(“8.gif”))
imageview.setBackgroundResource(R.drawable.a_8);
if (weather.equalsIgnoreCase(“9.gif”))
imageview.setBackgroundResource(R.drawable.a_9);
if (weather.equalsIgnoreCase(“10.gif”))
imageview.setBackgroundResource(R.drawable.a_10);
if (weather.equalsIgnoreCase(“11.gif”))
imageview.setBackgroundResource(R.drawable.a_11);
if (weather.equalsIgnoreCase(“12.gif”))
imageview.setBackgroundResource(R.drawable.a_12);
if (weather.equalsIgnoreCase(“13.gif”))
imageview.setBackgroundResource(R.drawable.a_13);
if (weather.equalsIgnoreCase(“14.gif”))
imageview.setBackgroundResource(R.drawable.a_14);
if (weather.equalsIgnoreCase(“15.gif”))
imageview.setBackgroundResource(R.drawable.a_15);
if (weather.equalsIgnoreCase(“16.gif”))
imageview.setBackgroundResource(R.drawable.a_16);
if (weather.equalsIgnoreCase(“17.gif”))
imageview.setBackgroundResource(R.drawable.a_17);
if (weather.equalsIgnoreCase(“18.gif”))
imageview.setBackgroundResource(R.drawable.a_18);
if (weather.equalsIgnoreCase(“19.gif”))
imageview.setBackgroundResource(R.drawable.a_19);
if (weather.equalsIgnoreCase(“20.gif”))
imageview.setBackgroundResource(R.drawable.a_20);
if (weather.equalsIgnoreCase(“21.gif”))
imageview.setBackgroundResource(R.drawable.a_21);
if (weather.equalsIgnoreCase(“22.gif”))
imageview.setBackgroundResource(R.drawable.a_22);
if (weather.equalsIgnoreCase(“23.gif”))
imageview.setBackgroundResource(R.drawable.a_23);
if (weather.equalsIgnoreCase(“24.gif”))
imageview.setBackgroundResource(R.drawable.a_24);
if (weather.equalsIgnoreCase(“25.gif”))
imageview.setBackgroundResource(R.drawable.a_25);
if (weather.equalsIgnoreCase(“26.gif”))
imageview.setBackgroundResource(R.drawable.a_26);
if (weather.equalsIgnoreCase(“27.gif”))
imageview.setBackgroundResource(R.drawable.a_27);
if (weather.equalsIgnoreCase(“28.gif”))
imageview.setBackgroundResource(R.drawable.a_28);
if (weather.equalsIgnoreCase(“29.gif”))
imageview.setBackgroundResource(R.drawable.a_29);
if (weather.equalsIgnoreCase(“30.gif”))
imageview.setBackgroundResource(R.drawable.a_30);
if (weather.equalsIgnoreCase(“31.gif”))
imageview.setBackgroundResource(R.drawable.a_31);
}

public void getWeather(String cityName) {
try {
SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
rpc.addProperty(“theCityName”, cityName);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.setOutputSoapObject(rpc);
HttpTransportSE ht = new HttpTransportSE(URL);

ht.debug = true;

ht.call(SOAP_ACTION, envelope);
detail = (SoapObject) envelope.getResponse();
parseWeather(detail);
} catch (Exception e) {
e.printStackTrace();
}
}

private void parseWeather(SoapObject detail)
throws UnsupportedEncodingException {
textview1 = (TextView) this.findViewById(R.id.TextView01);

String date = detail.getProperty(6).toString();

// 当天天气
weatherToday = “\n天气:” + date.split(” “)[1];
weatherToday = weatherToday + “\n气温:”
+ detail.getProperty(5).toString();
weatherToday = weatherToday + “\n风力:”
+ detail.getProperty(7).toString() + “\n”;

weatherNow = detail.getProperty(8).toString();
weatherWillBe = detail.getProperty(9).toString();

textview1.setText(getIp() + ‘\n’ + getCity() + “\n今天”
+ weatherToday);
setIcon(weatherNow, image1);
setIcon(weatherWillBe, image2);
}

5、最后在AndroidMainifest.xml加入权限

[java] view plaincopy
<uses-permission android:name=”android.permission.INTERNET”></uses-permission>
<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE”/>
<uses-permission android:name=”android.permission.ACCESS_WIFI_STATE”></uses-permission>
<uses-permission android:name=”android.permission.CHANGE_WIFI_STATE”></uses-permission>
<uses-permission android:name=”android.permission.WAKE_LOCK”></uses-permission>

推荐图书:Android开发实例大全、新手学HTML 5移动开发——面向iOS和Android、Google Android SDK开发范例大全(第3版)