Output JSON data:
public class Util {
//
public static String getString(String url) throws IOException {
try {
URL u = new URL(url);
//
URLConnection conn = u.openConnection();
InputStream in = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
StringBuffer sf = new StringBuffer();
String text = null;
while((text = br.readLine()) != null)
{
sf.append(text);
}
return sf.toString();
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
public static void main(String[] args) throws IOException {
String json = getString("https://itdage.cn/hw/weather?city=%E6%9D%AD%E5%B7%9E");
System.out.println(json);
}
}
Note:
//change to
String city = "北京" json = getString("https://itdage.cn/hw/weather?city="+URLEncoder.encode(city,"utf-8")
send message:
Test sending fixed content first
public static void main(String[] args) throws IOException {
String city = "北京";
String name="b( ̄▽ ̄)d 宝";
name = URLEncoder.encode(name,"utf-8");
// String json = getString("https://itdage.cn/hw/weather?city="+URLEncoder.encode(city,"utf-8"));
//z System.out.println(json);
//String phoneNumber = "19976883561";
String s1 = "天晴";
s1 = URLEncoder.encode(s1,"utf-8");
String s2 = "30-32";
String s3 = "Relex";
s2 = URLEncoder.encode(s2,"utf-8");
s3 = URLEncoder.encode(s3,"utf-8");
String json2 = getString("https://itdage.cn/hw/hwSms?name="+name+"&phoneNumber="+phoneNumber+"&s1="+s1+"&s2="+s2+"&s3="+s3);
System.out.println(json2);
}
Output OK, SMS can be seen on the mobile phone
Use one thread to execute the task:
public class SNStask {
private static Boolean flag;
private static Thread t1;
public static void start(long time,String name,String phoneNumber,String city)
{
if(!flag)
{
t1 = new Thread()
{
@Override
public void run() {
flag = true;
task:
while (flag) {
String text = Util.send(name, phoneNumber, city);
if (!"OK".equals(text)) {
continue;
}
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
break task;
}
}
}
};
t1.start();
}
}
public static void end(){
flag = false;
if(t1 != null)
{
t1.interrupt();
}
}
}
Set access path:
@WebServlet("/start")
public class StartServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, UnsupportedEncodingException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/json;charset=utf-8");
String name = request.getParameter("name");
String phoneNumber = request.getParameter("phoneNumber");
String city = request.getParameter("city");
SNStask.start(time,name,phoneNumber,city);
}
}
Read More:
- [Solved] JAVA HttpClient Send Https request certificate error:PKIX path building failed:
- Java: How to use itext to export PDF text absolute positioning (implementation method)
- JAVA Error: Error attempting to get column ‘XXX’ from result set. Cause java.sql.
- [Solved] Error attempting to get column ‘xxxx_time‘ from result set. Cause: java.sql.SQLFeatureNotSupportedEx
- How to get the current time in java time string
- How to Solve appium Startup Error (Various Error Messages)
- OpenFeignClient Use Object to Receive text/plain Type Return Error
- Java learning unreported exception java.io.IOException ; must be caught or declared to be thrown
- [Solved] Mybatis Error: attempting to get column ‘XX’ from result set
- [Solved] Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘;
- How to convert a Java string into a number (stringtonumber)
- The java springboot websocket service server actively closes the connection and causes java.io.EOFException to be thrown
- [Solved] Spring Kafka Send Error in specifies partition: Topic radar not present in metadata after 60000
- Java uses class array to report error Exception in thread “main” java.lang.NullPointerException solution
- Java implementation of inputsteam to Base64
- JAVA 8: How to Convert List to Map
- [Solved] Hibernate Error: java.lang.StackOverflowError at java.lang.Integer.toString(Integer.java:402)
- Java: How to Find the Minimum Value from a Random Array by Bubble Sort Method
- Java will convert Excel to list set or JSON, export excel file to local, excel import and export, easyexcel tool class