Android HTTP PUT请求 [英] Android HTTP PUT Request

查看:78
本文介绍了Android HTTP PUT请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以给我一个 HTTP PUT 请求Android的示例代码吗?

Can anyone give me a HTTP PUT request example code for Android?

推荐答案

假定您要使用HttpURLConnection,要执行HTTP PUT,请使用以下命令:

Assuming you want to use an HttpURLConnection, to perform an HTTP PUT you use the following:

URL url = new URL("http://www.example.com/resource");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("PUT");
OutputStreamWriter out = new OutputStreamWriter(
    httpCon.getOutputStream());
out.write("Data you want to put");
out.close();

要使用HTTPPut类,请尝试:

To use the HTTPPut class then try:

URL url = new URL("http://www.example.com/resource");
HttpClient client = new DefaultHttpClient();
HttpPut put= new HttpPut(url);

List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("key1", "value1"));
pairs.add(new BasicNameValuePair("key2", "value2"));
put.setEntity(new UrlEncodedFormEntity(pairs));

HttpResponse response = client.execute(put);

我很确定这应该可行,尽管我还没有测试过:)

I'm pretty sure this should work though I haven't tested it :)

这篇关于Android HTTP PUT请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆