Java 用飞信发短信,获取手机号码归属地,查询城市天气预报

前言:感谢那些为我们提供开源API的个人,团队,企业!敬礼!!!

简介:用开源apl完成发送手机飞信,查询号码归属地,查询天气。


功能:发飞信,查号码归属地,查天气

功能1:发飞信

FetionResult.java :处理返回数据结果

package Util;

public class FetionResult {
	private boolean ifSucceed;
	private String result;
	
	public FetionResult() {	}
	
	public FetionResult(boolean ifSucceed, String result) {
		this.ifSucceed = ifSucceed;
		this.result = result;
	}
	
	public boolean isIfSucceed() {
		return ifSucceed;
	}
	public void setIfSucceed(boolean ifSucceed) {
		this.ifSucceed = ifSucceed;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	
}

FetionSend.java 发送飞信

package Util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class FetionSend {
	
	private String user; //mobile number of your fetion
	private String passwd; // the password for your account of fetion
	private String sendTo; // who you want to send
	private String message; // the message content
	
	private static final String httpUrl = "http://2.smsfx.sinaapp.com/send.php";
	

	public String getUser() {
		return user;
	}


	public void setUser(String user) {
		this.user = user;
	}


	public String getPasswd() {
		return passwd;
	}


	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}


	public String getSendTo() {
		return sendTo;
	}


	public void setSendTo(String sendTo) {
		this.sendTo = sendTo;
	}


	public String getMessage() {
		return message;
	}


	public void setMessage(String message) {
		this.message = message;
	}
	
	public FetionResult send() throws IOException {
		FetionResult result = new FetionResult();
		result.setIfSucceed(false);
		
		if ("".equals(user) || user == null) {
			result.setResult("The user name can‘t be empty!");
			return result;
		}
		
		if ("".equals(passwd) || passwd == null) {
			result.setResult("The password can‘t be empty!");
			return result;
		}
		
		if ("".equals(sendTo) || sendTo == null) {
			result.setResult("The number you send to can‘t be empty!");
			return result;
		}
		
		if ("".equals(message) || message == null) {
			result.setResult("The message content can‘t be empty!");
			return result;
		}
		
		String getUrl = new StringBuffer(httpUrl).append("?tel=").append(user).append("&pwd=").append(passwd)
						.append("&aim=").append(sendTo).append("&text=").append(URLEncoder.encode(message,"utf-8")).toString();
		URL urlLocate = new URL(getUrl);
		HttpURLConnection connection = (HttpURLConnection) urlLocate.openConnection();
		connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded,charset=utf-8");
		connection.connect();
		BufferedReader reader = new BufferedReader( new InputStreamReader(connection.getInputStream()));
		
		String lineResult;
		while ((lineResult = reader.readLine()) != null) {
			System.out.println(lineResult);
		}
		
		return result;
	}


}

功能2:查询号码归属地

这个返回的结果是json,所以得先学会怎么处理json

java处理json数据

JsonUtil.java 将返回的json存储为字符串

package Util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class JsonUtil {
 

    public static String loadJSON (String url) {
        StringBuilder json = new StringBuilder();
        try {
        	URL urlLocate = new URL(url);
    		HttpURLConnection connection = (HttpURLConnection) urlLocate.openConnection();
    		connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded,charset=utf8");
    		connection.connect();
            BufferedReader in = new BufferedReader(new InputStreamReader(
            		connection.getInputStream()));
            String inputLine = null;
            while ( (inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
        return json.toString();
    }
    
    
}


__GetZoneResult_.java 存数返回的手机号码归属地数据

package dao;

public class __GetZoneResult_ {
	private String mts;
	private String province;
	private String catName;
	private String telString;
	private String areaVid;
	private String ispVid;
	public String getMts() {
		return mts;
	}
	public void setMts(String mts) {
		this.mts = mts;
	}
	public String getProvince() {
		return province;
	}
	public void setProvince(String province) {
		this.province = province;
	}
	public String getCatName() {
		return catName;
	}
	public void setCatName(String catName) {
		this.catName = catName;
	}
	public String getTelString() {
		return telString;
	}
	public void setTelString(String telString) {
		this.telString = telString;
	}
	public String getAreaVid() {
		return areaVid;
	}
	public void setAreaVid(String areaVid) {
		this.areaVid = areaVid;
	}
	public String getIspVid() {
		return ispVid;
	}
	public void setIspVid(String ispVid) {
		this.ispVid = ispVid;
	}
	

}

PhoneUtil.java 获取手机归属地

package Util;


import com.google.gson.Gson;

import dao.__GetZoneResult_;

public class PhoneUtil {
	
	public static __GetZoneResult_ getresult(String tel)
	{
        String url = "http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel="+tel+"";
        String json = JsonUtil.loadJSON(url);
        json=json.substring(json.indexOf("=")+1);
        Gson gson=new Gson();
        __GetZoneResult_ getresult=gson.fromJson(json, dao.__GetZoneResult_.class);
        return getresult;
	}
}

功能3:查询天气

WeatherInfo.java 存储天气信息

package dao;

public class WeatherInfo {
	//{"weatherinfo":{"city":"????","cityid":"101010100","temp1":"12??","temp2":"28??","weather":"???????","img1"
	//:"n53.gif","img2":"d1.gif","ptime":"18:00"}}
	private String city;
	private String cityid;
	private String temp1;
	private String temp2;
	private String weather;
	private String ptime;
	private String img1;
	private String img2;
	public String getImg1() {
		return img1;
	}
	public void setImg1(String img1) {
		this.img1 = img1;
	}
	public String getImg2() {
		return img2;
	}
	public void setImg2(String img2) {
		this.img2 = img2;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getCityid() {
		return cityid;
	}
	public void setCityid(String cityid) {
		this.cityid = cityid;
	}
	public String getTemp1() {
		return temp1;
	}
	public void setTemp1(String temp1) {
		this.temp1 = temp1;
	}
	public String getTemp2() {
		return temp2;
	}
	public void setTemp2(String temp2) {
		this.temp2 = temp2;
	}
	public String getWeather() {
		return weather;
	}
	public void setWeather(String weather) {
		this.weather = weather;
	}
	public String getPtime() {
		return ptime;
	}
	public void setPtime(String ptime) {
		this.ptime = ptime;
	}
	

}


WeatherUtil.java

package Util;



import com.google.gson.Gson;

import dao.WeatherInfo;

public class WeatherUtil {
	public static WeatherInfo getWeatherInfo(String cityId)
	{
		String url = "http://www.weather.com.cn/data/cityinfo/"+cityId+".html";
        String json = JsonUtil.loadJSON(url);
        json=json.substring(json.indexOf(":")+1,json.length()-1);
        System.out.println(json);
        Gson gson=new Gson();
        WeatherInfo getresult=gson.fromJson(json, dao.WeatherInfo.class);
        return getresult;
	}

}

测试:查询天气后用飞信发送

package test;


import java.io.IOException;

import org.junit.Test;

import dao.WeatherInfo;

import Util.FetionSend;
import Util.WeatherUtil;

public class TestWeather {

	@Test
	public void test() {
		String cityID="101120601";
		WeatherInfo weather=WeatherUtil.getWeatherInfo(cityID);
		FetionSend fetion = new FetionSend();
		fetion.setUser("18253591716");
		fetion.setPasswd("***********");
		fetion.setSendTo("18253591716");
		fetion.setMessage("城市:"+weather.getCity()+" 	最低温度:"+weather.getTemp1()+
				" 	最高温度:"+weather.getTemp2()+" 	天气状况:"+weather.getWeather()+" 	发布时间:"+weather.getPtime());
		System.out.println(fetion.getMessage());
		try {
			fetion.send();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

感悟:就那么简单,不需要太多的jar包,只需要一个解析JSON的

下载链接

gson-2.2.4.jar

相关源码



Java 用飞信发短信,获取手机号码归属地,查询城市天气预报,布布扣,bubuko.com

Java 用飞信发短信,获取手机号码归属地,查询城市天气预报

上一篇:[Android]Android开发入门之HelloWorld


下一篇:Android中handler运行原理