如何从Android的活动提交的变量,以网站的网址? [英] How do I submit variables from Android Activity to website url?
问题描述
我已经建立了一个应用程序,它就像一个形式,它需要四个字段,并验证信息,以确保没有无效字符输入。这四个字段存储在变量:
I have an application built that works like a form, it takes four fields and validates the information in order to make sure that no invalid characters are entered. These four fields are stored in variables:
- 电话
- 名称
- 电子邮件
-
评论
- Phone
- Name
Comments
现在我想表单数据(无论是进入这四个领域,并存储到变量)提交的URL(将使用的 http://www.test.com ),但我不知道如何去这样做。我想我在寻找所谓的HttpURLConnection的,但我不知道如何指定哪个变量被发送。下面我将code发现从网站 http://developer.android。 COM /参考/爪哇/ NET / HttpURLConnection.html
Now I want to submit the form data (whatever is entered into these four fields and stored to the variables) to a url (will use http://www.test.com), but I am not sure how to go about doing this. I think I am looking for something called the HttpURLConnection, but I am not sure how to specify which variable is sent. The code below I found from the website http://developer.android.com/reference/java/net/HttpURLConnection.html
private class UploadFilesTask extends AsyncTask<URL, Integer, Long>{
protected Long doInBackground(URL... urls) {
try {
HttpClient http = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.test.com");
List<NameValuePair> data = new ArrayList<NameValuePair>();
data.add(new BasicNameValuePair("phone", "value"));
data.add(new BasicNameValuePair("name", "value"));
data.add(new BasicNameValuePair("email", "value"));
data.add(new BasicNameValuePair("comments", "value"));
post.setEntity(new UrlEncodedFormEntity(data));
HttpResponse response = http.execute(post);
// do something with the response
}
catch (ClientProtocolException e) {
// do something
finish();
}
catch (IOException e) {
// do something
finish();
}
}
}
任何帮助将是非常美联社preciated,谢谢!
Any help would be much appreciated, thank you!
推荐答案
将表单数据发送到服务器的最简单方法是使用HttpClient和HttpPost。
The simplest way to send form data to a server is to use HttpClient, and HttpPost.
尝试是这样的:
try {
HttpClient http = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.example.com/process");
List<NameValuePair> data = new ArrayList<NameValuePair>();
data.add(new BasicNameValuePair("phone", "value");
data.add(new BasicNameValuePair("name", "value");
data.add(new BasicNameValuePair("email", "value");
data.add(new BasicNameValuePair("comments", "value");
post.setEntity(new UrlEncodedFormEntity(data));
HttpResponse response = http.execute(post);
// do something with the response
}
catch (ClientProtocolException e) {
// do something
}
catch (IOException e) {
// do something
}
请注意,你要在AsyncTask的执行此操作,这样你就不会锁定在UI线程等待网络操作完成。
Note, you'll want to perform this operation in an AsyncTask so you don't lock up the UI thread waiting for the networking operations to complete.
修改
下面是它可能看起来像在AsyncTask的一个简单快速的例子。
Here's a simple quick example of what it might look like in an AsyncTask.
public class SendTask extends AsyncTask<Void, Void, Boolean> {
String responseString;
@Override
protected Boolean doInBackground(Void... params) {
try {
HttpClient http = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.test.com");
List<NameValuePair> data = new ArrayList<NameValuePair>();
data.add(new BasicNameValuePair("phone", "value"));
data.add(new BasicNameValuePair("name", "value"));
data.add(new BasicNameValuePair("email", "value"));
data.add(new BasicNameValuePair("comments", "value"));
post.setEntity(new UrlEncodedFormEntity(data));
HttpResponse response = http.execute(post);
responseString = new BasicResponseHandler().
handleResponse(response); // Basic handler
return true;
}
catch (ClientProtocolException e) {
// do something useful to recover from the exception
// Note: there may not be anything useful to do here
}
catch (IOException e) {
// do something useful to recover from the exception
// Note: there may not be anything useful to do here
}
return false;
}
@Override
protected void onPostExecute(Boolean success) {
// TODO: Do something more useful here
if (success) {
Toast.makeText(MainActivity.this, "Success: " + responseString, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_LONG).show();
}
}
}
这篇关于如何从Android的活动提交的变量,以网站的网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!