JavaFX基礎學習之OkHttp/Gson2

來源:互聯網
上載者:User

標籤:tar   style   util   cti   內容   使用者id   catch   code   row   

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.