Ash*_*win 4 android urlconnection http-post
我正在使用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();
Run Code Online (Sandbox Code Playgroud)
但我看到很多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);
Run Code Online (Sandbox Code Playgroud)
请解释两者之间的差异.如何在servlet中接收后一种情况下的数据.请对这个HttpPost做一个简短的解释.在互联网上,我找到的只是代码.请逐步解释HttpPost及其方法,以及如何在servlet中接收数据.链接会很好.
这篇博文非常好地解释了它们之间的区别(实际上是HttpURLConnection,但这只是URLConnection的一个子类).本文的一些亮点是:
虽然文章的结尾建议在froyo之上的所有平台上使用HttpURLConnection,但我个人喜欢使用HttpClient而不管是什么.它对我来说更容易使用,更有意义.但是如果你已经在使用HttpURLConnection,你应该完全继续使用它.从这里开始,它将从Android开发人员那里获得很多爱.
| 归档时间: |
|
| 查看次数: |
3609 次 |
| 最近记录: |