0

Can someone help me figure out how to translate the following curl commands into Java syntax for use in an Android application?

The curl commands are:

curl -u username:password -H "Content-Type:application/vnd.org.snia.cdmi.container" http://localhost:8080/user

curl -u username:password -T /home/user1/a.jpg -H "Content-Type:image" http://localhost:8080/user/a.jpg

thanks

sudo
  • 1,525
  • 7
  • 34
  • 59

1 Answers1

2

You can use Apache HttpClient in Android for performing HTTP posts.

  • HttpClient code snippet (untested)

    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
    } 
    

    }

  • Check the following link for the basic authentication part (your -u option)

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

  • Check the following answer for your file upload (your -T option)

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

Community
  • 1
  • 1
ddewaele
  • 22,363
  • 10
  • 69
  • 82
  • hi ddewaele, i use PUT instead of post to upload file. it is same just change HttpPut ? thanks – sudo May 23 '11 at 06:13