222

package application;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import com.google.gson.Gson;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Controller implements Initializable{

    @FXML
    private Button button;
    @FXML
    private TextField textfield;
    @FXML
    private TextArea textarea;

    private static OkHttpClient client = new OkHttpClient();

    //发起请求,获取内容
    public static String getjson (String url) throws IOException {
          Request request = new Request.Builder()
              .url(url)
              .build();

          Response response = client.newCall(request).execute();
          return response.body().string();
        }

    //函数调用api,并利用gson解析json数据,获取KEY
    public static String getkey(String corpid,String corpsecret){

        String json = null;
        try{

            json = getjson("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpid+"&corpsecret="+corpsecret);

        }catch (Exception e){

            e.printStackTrace();
        }

        Gson gson = new Gson();

        weixinapi wxapi = gson.fromJson(json, weixinapi.class);

        return wxapi.getAccess_token();

    }

      //函数调用api,并利用gson解析json数据,获取用户ID
      public static String[] getid(String token,String xuehao){

        String json = null;
        try{

            json = getjson("https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token="+token+"&userid="+ xuehao);

        }catch (Exception e){

            e.printStackTrace();
        }

        System.out.println(json);

        if (json.contains("ok")){
                Gson gson = new Gson();

                idapi iapi = gson.fromJson(json, idapi.class);

                String xb = null;

                 if (iapi.getGender().equals("1")){
                            xb = "男";

                    }else if (iapi.getGender().equals("2")){
                            xb = "女";
                  }

                return new String[]{
                    "学号: "+ iapi.getUserid(),
                    "姓名:" + iapi.getName(),
                    "手机号: "+ iapi.getMobile(),
                    "性别 : "+ xb,
                    "微信号: "+ iapi.getWeixinid()    

                };

        }else {
            return new String[]{ 

                    "user not find",

            };}

    }

    @FXML
    private void handleButtonAction(ActionEvent event) {

        String key=getkey("test","test");
        String xuehao = textfield.getText();

        for(String str : getid(key,xuehao)){

              textarea.appendText(str+"\n");

        }

    }

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub

    }

}

最新文章

  1. 表值函数与JS中split()的联系
  2. codevs1002 搭桥
  3. ASP.NET MVC下的四种验证编程方式
  4. JavaScript 鼠标划过 播放音乐。
  5. android studio学习之一
  6. HTML4 和 HTML5 的10个关键区别
  7. 【windows核心编程】DLL相关(2)
  8. 转载 深入理解JavaScript中的this关键字
  9. myeclipse输入“.”后没有自动提示功能
  10. Scut 上线后遇到的问题
  11. 【ajax】reqwest
  12. Mysql 卡死的处理办理
  13. unix c 03
  14. 用jQuery实现鼠标在table上移动进行样式变化
  15. HAUT--1262--魔法宝石(暴力)
  16. 关于asp.net web form 和 asp.net mvc 的区别
  17. [原创]基于Zynq PS与PL之间寄存器映射 Standalone & Linux 例程
  18. Spring Cloud微服务笔记(二)Spring Cloud 简介
  19. Sun SPARC Enterprise M5000 启动步骤
  20. python进阶(一)

热门文章

  1. Android Device Chooser中显示Target unknown解决方法
  2. java类静态域、块,非静态域、块,构造函数的初始化顺序
  3. es6 Module
  4. php目录函数
  5. 数据意识崛起,从企业应用看BI软件的未来发展
  6. CodeForces 749D Leaving Auction
  7. Linux环境快速搭建RocketMQ双Master模式
  8. CentOS Gnome 识别 NTFS-3G
  9. flexbox备忘
  10. BGP服务器您了解多少?