Make an HTTP request with android

后端 未结 12 1090
时光取名叫无心
时光取名叫无心 2020-11-21 06:38

I have searched everywhere but I couldn\'t find my answer, is there a way to make a simple HTTP request? I want to request a PHP page / script on one of my websites but I do

12条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-21 07:18

    I made this for a webservice to requerst on URL, using a Gson lib:

    Client:

    public EstabelecimentoList getListaEstabelecimentoPorPromocao(){
    
            EstabelecimentoList estabelecimentoList  = new EstabelecimentoList();
            try{
                URL url = new URL("http://" +  Conexao.getSERVIDOR()+ "/cardapio.online/rest/recursos/busca_estabelecimento_promocao_android");
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
    
                if (con.getResponseCode() != 200) {
                        throw new RuntimeException("HTTP error code : "+ con.getResponseCode());
                }
    
                BufferedReader br = new BufferedReader(new InputStreamReader((con.getInputStream())));
                estabelecimentoList = new Gson().fromJson(br, EstabelecimentoList.class);
                con.disconnect();
    
            } catch (IOException e) {
                e.printStackTrace();
            }
            return estabelecimentoList;
    }
    

提交回复
热议问题