[PROJECT] itdage java to get the weather and send text messages

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: