JavaFX基础学习之OkHttp/Gson2

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaFX基础学习之OkHttp/Gson2相关的知识,希望对你有一定的参考价值。

 

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
        
    }
    
    

}

 

以上是关于JavaFX基础学习之OkHttp/Gson2的主要内容,如果未能解决你的问题,请参考以下文章

JavaFX基础学习之OkHttp/Gson

JavaFX学习之TableView

Python基础学习之语句和语法

python 学习之 基础篇三 流程控制

Scala基础学习之for循环和while循环

python学习之第八篇:Python中的函数基础