2011-05-23 49 views

回答

2

可以在Android上使用Apache HttpClient執行HTTP崗位。

  • HttpClient的代碼片段(未經測試)

    public void postData(String url) { 
        // Create a new HttpClient and Post Header 
        HttpClient httpclient = new DefaultHttpClient(); 
        HttpPost httppost = new HttpPost(url); 
    
    try { 
        // Set the headers (your -H options) 
        httpost.setHeader("Content-type", "application/json"); 
        // Add your data 
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
        nameValuePairs.add(new BasicNameValuePair("param1", "value1")); 
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
    
        // Execute HTTP Post Request 
        HttpResponse response = httpclient.execute(httppost); 
    
    } catch (Exception e) { 
        // Exception handling 
    } 
    

    }

  • 查看以下鏈接的基本身份驗證部分(您-u選項)

http://dlinsin.blogspot.com/2009/08/http-basic-authentication-with-android.html

  • 檢查如下回答您的文件上傳(您-T選項)

How to upload a file using Java HttpClient library working with PHP

+0

喜ddewaele,我用PUT而不是發佈上傳文件。它只是改變HttpPut?謝謝 – sudo 2011-05-23 06:13:37

+0

庫也支持PUT(PutMethod vs PostMethod)。應該管用... – ddewaele 2011-05-23 06:18:14