Android的HttpPut例如code [英] Android HttpPut example code

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

问题描述

任何人都可以给我一个httpPut请求,例如code?

could anyone give me a httpPut request example code?

推荐答案

假设你想使用一个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);

我是pretty的肯定,这应该工作,虽然我没有测试它:)

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

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

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