将URL拆分为主机和路径

时间:2016-10-23 16:50:48

标签: java

我有一个程序,其中有一个按钮,用于打印出网址的html文本。我有一个变量,文本框中已经有一个地址,问题是我是否更改了文本框中的文本到另一个地址,它仍将打印出相同的html地址。我已经严厉地说过这个问题,但希望这是可以理解的。我希望能够输入一个URL并获取该地址html,而不是进入代码eveytime并手动更改变量。

/* Create a HttpInteract object. */
    public HttpInteract(String url) {

        /* Split the "URL" into "host name" and "path name", and
        * set host and path class variables. 
        * if URL is only a host name, use "/" as path 
        */  
        System.out.println("URL splits into host name and path name.");
        host = "cgi.csc.liv.ac.uk";
        System.out.println("Host is:" +host);
        path = "/~gairing/test.txt";
        System.out.println("Path is:" +path);        

        //Request message. Connection closes after response because http 1.0
        //is non persistent
        requestMessage= "GET "+path+ " HTTP/1.1\r\n"
                        +"Host: " +host+  "\r\n"
                        +"\r\n" ;

        return;
    }   

1 个答案:

答案 0 :(得分:2)

使用URL的简单示例:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_ow);
       Button button1=(Button) findViewById(R.id.tac);

        final Spinner spin = (Spinner) findViewById(R.id.spinner);  
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = spin.getSelectedItem().toString();

                if (text.equals("GTB")) {
                    Intent intent = new Intent(ow.this, gtb.class);
                    startActivity(intent);
                }else if(text.equals("ZENITH")){
                    Intent intent=new Intent(v.getContext(),z.class);
                    startActivity(intent);
                }
            }

        });

    }}