gpt4 book ai didi

java - 如何将 Android Activity 中的变量提交到网站 url?

转载 作者:太空狗 更新时间:2023-10-29 16:22:12 25 4
gpt4 key购买 nike

我构建了一个像表单一样工作的应用程序,它有四个字段并验证信息以确保没有输入无效字符。这四个字段存储在变量中:

  • 电话
  • 姓名
  • 电子邮件
  • 评论

    现在我想将表单数据(输入到这四个字段并存储到变量中的任何内容)提交到一个 url(将使用 http://www.test.com ),但我不确定如何去做。我想我正在寻找称为 HttpURLConnection 的东西,但我不确定如何指定发送哪个变量。下面的代码是我从网站上找到的 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();
    }

非常感谢任何帮助,谢谢!

最佳答案

将表单数据发送到服务器的最简单方法是使用 HttpClient 和 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 线程。

编辑

这是一个简单的快速示例,说明它在 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();
}
}
}

关于java - 如何将 Android Activity 中的变量提交到网站 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11614456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com