安卓:使用HTTPGET发送URL [英] Android: Using HttpGet to send a URL

查看:215
本文介绍了安卓:使用HTTPGET发送URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个网址发送到使用的值来自领域的EditText我的服务器。目前,它继续强制关闭按钮点击。这是我有:

I'm trying to send a URL to my server that uses values from edittext fields. Currently it continues to force close on the button click. This is what I have:

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();
        }
    }
});

我还没有把在&lt中的EditText值;>的URL,但我认为不会造成任何问题。为什么不将它发送的网址?

I haven't put the edittext values in the <> of the URL yet, but I assume that wouldn't cause any problems. Why won't it send the URL?

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

I don't want it to open a browser or anything like that but send the URL in the background and then I would have an intent that would return them to their previous screen.

推荐答案

在你没有发送任何的时刻。

At the moment you are not sending anything.

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 这两个工作;)

这篇关于安卓:使用HTTPGET发送URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆