从Android中的互联网链接获取数据
问题描述:
我正在创建一个带有URL的应用程序。 * .asp扩展名,我们传递所需的参数,并使用POST方法得到一些字符串结果。
I am making an application which takes a URL with. *.asp extension and we pass it the required parameters and get some string result using POST method.
有关如何实现此目的的任何建议吗?
Any suggestions on how to achieve this?
更新:
实际上我有.net链接需要一些POST参数并给我一个结果。我怎么能在Android中做到这一点?
Actually I have a .net link which takes some POST Parameters and gives me a Result. How can I do that in Android?
答
HTTPResponse可以解决这个问题:
HTTPResponse should do the trick:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoururl.com");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); <!-- number should be the amount of parameters
nameValuePairs.add(new BasicNameValuePair("nameOfParameter", "parameter"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
InputStream is = buf.getContent();
现在你有一个可以使用的流,将数据写入字符串:
Now you got a stream to work with, to write the data to a string:
BufferedReader r = new BufferedReader(new InputStreamReader(is));
total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}
祝你好运!