Make an HTTP request with android

后端 未结 12 1104
时光取名叫无心
时光取名叫无心 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:20

    With a thread:

    private class LoadingThread extends Thread {
        Handler handler;
    
        LoadingThread(Handler h) {
            handler = h;
        }
        @Override
        public void run() {
            Message m = handler.obtainMessage();
            try {
                BufferedReader in = 
                    new BufferedReader(new InputStreamReader(url.openStream()));
                String page = "";
                String inLine;
    
                while ((inLine = in.readLine()) != null) {
                    page += inLine;
                }
    
                in.close();
                Bundle b = new Bundle();
                b.putString("result", page);
                m.setData(b);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            handler.sendMessage(m);
        }
    }
    

提交回复
热议问题