安卓:使用HTTPGET发送URL [英] Android: Using HttpGet to send a 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屋!