如何将Android Activity中的变量提交到网站网址?

teh*_*man 2 java android http-post httpurlconnection

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

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

    现在我想将表单数据(无论是输入这四个字段并存储到变量中)提交到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();
        }
    
    Run Code Online (Sandbox Code Playgroud)

    }

    }

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

nEx*_*are 5

将表单数据发送到服务器的最简单方法是使用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
}
Run Code Online (Sandbox Code Playgroud)

注意,您需要在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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)