Android:使用HttpGet发送URL

时间:2011-06-14 14:56:45

标签: android url http-get

我正在尝试向我的服务器发送URL,该服务器使用来自edittext字段的值。目前它继续强制关闭按钮点击。这就是我所拥有的:

Button testbtn = (Button) findViewById(R.id.Testbtn);
testbtn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        HttpGet method = new HttpGet("http://mysite.com/test.php?first=<>&last=<>&address=<>&phone=<>&zip=<>&email=<>");
        HttpClient client = new DefaultHttpClient();
        try {
            client.execute(method);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
});

我没有将edittext值放在&lt;&gt;中的URL,但我认为这不会导致任何问题。为什么不发送URL?

我不希望它打开浏览器或类似的东西,但在后台发送URL,然后我会有一个意图,将它们返回到他们以前的屏幕。

2 个答案:

答案 0 :(得分:9)

目前你没有发送任何东西。

HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/");

    try {
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        HttpEntity ht = response.getEntity();

            BufferedHttpEntity buf = new BufferedHttpEntity(ht);

            InputStream is = buf.getContent();

            BufferedReader r = new BufferedReader(new InputStreamReader(is));

            StringBuilder total = new StringBuilder();
            String line;
            while ((line = r.readLine()) != null) {
                total.append(line);
            }

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

希望这有帮助!

<强>更新

HttpPost更改为HttpGet两者都有效;)

答案 1 :(得分:1)

client为空。你必须初始化它。