Httpurlconnetion get

xiaoxiao2021-02-28  148

//相当于你们在打开浏览器,地址栏填网址             URL url = new URL("http://apis.juhe.cn/cook/query.php?menu=秘制红烧肉&key=ff00d7339861c7fd7d5b54b16b76422a");             //相当于你们敲回车键            HttpURLConnection connection = (HttpURLConnection) url.openConnection();             //设置请求方式  GET / POST  请求方式字母必须大写             connection.setRequestMethod("GET");             //设置连接服务器时间             connection.setConnectTimeout(5000);             //设置读取数据的时间             connection.setReadTimeout(5000);             //等待服务响应             //得到服务响应的状态码 1xx  2xx 3xx 4xx 5xx  200 OK 404  500             int code = connection.getResponseCode();             if(code == 200){//服务响应成功                 //获取数据                InputStream is = connection.getInputStream();                 String json = StreamTools.readFromNetWork(is);                 Gson gson = new Gson();                 MenuInfo menuInfo = gson.fromJson(json, MenuInfo.class);
转载请注明原文地址: https://www.6miu.com/read-17984.html

最新回复(0)