安卓okhttp之GET请求


new Thread(new Runnable() {
     
    @Override
    public void run() {
     
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(GlobalData.httpUrl + "user/login?email="+email.getText()+"&password="+password.getText()).build();
        try {
     
            Response response = client.newCall(request).execute();//发送请求
            try {
     
                JSONObject result = new JSONObject(response.body().string());
            } catch (Exception e) {
     
                e.printStackTrace();
            }
        } catch (IOException e) {
     
            e.printStackTrace();
        }
    }
}).start();

注意:response.body().string()只能调用一次,否则会报IllegalStateException: closed。

你可能感兴趣的:(学习笔记)