在android客户端中使用URLCOnnection对象和Httppost之间的区别

发布于 2025-01-07 20:07:52 字数 1001 浏览 0 评论 0原文

我正在使用 URLConnection 对象将数据从 Android 客户端发送到服务器。

URL url = new URL("http://10.0.2.2:8080/hello");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
ObjectOutputStream out=new ObjectOutputStream(connection.getOutputStream());
String s="check"+","+susername;
out.writeObject(s);
out.flush();
out.close();

但我见过很多android程序通过以下方式使用httppost发送数据。

HttpClient client=new DefaultHttpClient();
HttpPost httpPost=new HttpPost(LOGIN_ADDRESS);
List pairs=new  ArrayList();
String strUsername=username.getText().toString();
String strPassword=password.getText().toString();
pairs.add(new BasicNameValuePair("username", strUsername));
pairs.add(new BasicNameValuePair("password", strPassword));
httpPost.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response= client.execute(httpPost);

请解释两者之间的区别。在后一种情况下,您将如何在 servlet 中接收数据。请对这个HttpPost做一个简单的解释。在互联网上我找到的只是代码。请逐步解释 HttpPost 及其方法以及 servlet 中应如何接收数据。链接会很好。

I am using URLConnection Object to send data from my android client to server.

URL url = new URL("http://10.0.2.2:8080/hello");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
ObjectOutputStream out=new ObjectOutputStream(connection.getOutputStream());
String s="check"+","+susername;
out.writeObject(s);
out.flush();
out.close();

But I have seen many android programs sending data using httppost in the following way.

HttpClient client=new DefaultHttpClient();
HttpPost httpPost=new HttpPost(LOGIN_ADDRESS);
List pairs=new  ArrayList();
String strUsername=username.getText().toString();
String strPassword=password.getText().toString();
pairs.add(new BasicNameValuePair("username", strUsername));
pairs.add(new BasicNameValuePair("password", strPassword));
httpPost.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response= client.execute(httpPost);

please explain the differnce between the two. How would you receive the data in the later case in a servlet. please give a brief explanation on this HttpPost. In the internet all I find is code. pls give a step by step explanation on HttpPost and its methods and how should the data be received in the servlet. Links will do fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心房敞 2025-01-14 20:07:53

这篇博客文章很好地解释了两者之间的区别它们(实际上是 HttpURLConnection,但这只是 URLConnection 的子类)。本文的一些亮点是:

  • HttpURLConnection 轻松允许 gzip 编码
  • HttpURLConnection 可以轻松缓存结果
  • HttpURLConnection 较新,并且正在积极开发,因此它只会变得更快更好
  • HttpURLConnection 在 foryo 和 pre-froyo 平台上有一些恼人的错误
  • HttpClient是经过验证的。它已经存在很长一段时间了,而且它的工作原理
  • HttpClient 几乎没有被开发,因为它太旧了并且 API 被完全锁定。 Android 开发者在使其变得更好方面无能为力。

虽然文章最后建议在 froyo 以上的所有平台上使用 HttpURLConnection,但无论如何我个人还是喜欢使用 HttpClient。它对我来说更容易使用并且更有意义。但如果您已经在使用 HttpURLConnection,则完全应该继续使用它。从现在开始,它将受到 Android 开发者的广泛喜爱。

This blog post does a pretty good job of explaining the difference between the two of them (well actually HttpURLConnection, but that's just a subclass of URLConnection). Some highlights from the article are:

  • HttpURLConnection easily allows gzip encoding
  • HttpURLConnection can allow for easy caching of results
  • HttpURLConnection is newer and being actively developed on so it's only going to get faster and better
  • HttpURLConnection has some annoying bugs on foryo and pre-froyo platforms
  • HttpClient is tried and true. It's been around for a long time and it works
  • HttpClient is pretty much not being developed on because it's so old and the API is completely locked down. There isn't much more the android developers can do in terms of making it better.

While the end of the article recommends use of HttpURLConnection on all platforms above froyo, I personally like using HttpClient no matter what. It's just easier to use for me and makes more sense. But if you're already using HttpURLConnection, you should totally keep using it. It's going to be receiving lot's of love from the android developers from here-on-out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文