Android HTTP PUT Request

后端 未结 2 653
粉色の甜心
粉色の甜心 2021-02-02 01:14

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

2条回答
  •  梦毁少年i
    2021-02-02 02:10

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

    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 pairs = new ArrayList();
    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 :)

提交回复
热议问题