1. 程式人生 > >timer定時器每天某個時間更新、java訪問外部介面方法

timer定時器每天某個時間更新、java訪問外部介面方法

        private void updateFxsmc()  {
                 String bb="";  //儲存返回資訊
                 String uri = ""; //請求地址
                 Properties prop = new Properties();
                 try {
                          prop.load(this.getClass().getResourceAsStream("/res/classify.properties"));
                          CloseableHttpClient httpclient = HttpClients.createDefault();
                         uri = prop.getProperty("classifyUrl");
                         HttpPost httppost = new HttpPost(uri);
                         //設定請求頭
                         httppost.setHeader("Content-Type", "application/json;charset=UTF-8");
                         //建立請求引數
                          Map<String,String> param = new HashMap<String,String>();
 
                          String json = JSON.toJSONString(param);
                          StringEntity se = new StringEntity(json);
                          se.setContentType(CONTENT_TYPE_TEXT_JSON);         
                          //放入通道中,待傳遞
                         httppost.setEntity(se);
                          //7:讓客戶端執行請求(帶有資料的請求),得到的是CloseableHttpResponse物件(響應物件)
                          CloseableHttpResponse responOrder = httpclient.execute(httppost); 
                         int codeResponse = responOrder.getStatusLine().getStatusCode();
                         if(codeResponse == 200){
                                  //傳遞成功
                                     HttpEntity entity2 = responOrder.getEntity();
                                     byte[] b = EntityUtils.toByteArray(entity2);
                                 //輸出內容
                                     bb = new String(b, "utf-8");
                                 //傳遞給前段
                                     JSONObject bbJson = JSON.parseObject(bb);
                                     long start = 0;
                                 //判斷是否返回異常
                                 String sql ="INSERT INTO classify(aaa,bbb) VALUE ";
                                 if(Integer.parseInt(bbJson.getString("code"))==0){            
                                          JSONArray result = bbJson.getJSONObject("result").getJSONArray("data");
                                          /**************先插入資料,然後更改**********************/
                                          StringBuffer suffix = new StringBuffer();
                                          for (int i = 0; i < result.size(); i++) {
         
                                                   JSONObject value = result.getJSONObject(i);
                                                   String aaa =  value.getString("aaa") ;
                                                   String bbb = value.getString("bbb");
                                                   if(!bbb.equals("")){
                                                                suffix.append("("+aaa+",\""+bbb+"\"),");
                                                   }else{
                                                                continue;
                                                   }         
                                          }
                                          sql = sql+suffix.substring(0, suffix.length() - 1);
                                          jdbcConnect(sql);
              
                                 }else{
                                           System.out.println("返回結果中,code=1");
                                 }
           
                         }else{
                                 System.out.println("網頁錯誤程式碼:"+codeResponse);
                         }
                     }catch (Exception e) {
                          e.printStackTrace();
             }