gpt4 book ai didi

Android HTTP PUT 请求

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:55:39 25 4
gpt4 key购买 nike

谁能给我一个Android的HTTP PUT请求示例代码?

最佳答案

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

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 类,请尝试:

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);

虽然我还没有测试过,但我很确定这应该有效:)

关于Android HTTP PUT 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3649814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com