前言:感谢那些为我们提供开源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.Malforme