CURL in android CURL in android curl curl

CURL in android


Let's assume that you want to do the following request:

curl -u user:password http://sample.campfirenow.com/rooms.xml

In Android you would do as follow.

public static String getRequest() {        StringBuffer stringBuffer = new StringBuffer("");        BufferedReader bufferedReader = null;        try {            HttpClient httpClient = new DefaultHttpClient();            HttpGet httpGet = new HttpGet();            URI uri = new URI("http://sample.campfirenow.com/rooms.xml");            httpGet.setURI(uri);            httpGet.addHeader(BasicScheme.authenticate(                    new UsernamePasswordCredentials("user", "password"),                    HTTP.UTF_8, false));            HttpResponse httpResponse = httpClient.execute(httpGet);            InputStream inputStream = httpResponse.getEntity().getContent();            bufferedReader = new BufferedReader(new InputStreamReader(                    inputStream));            String readLine = bufferedReader.readLine();            while (readLine != null) {                stringBuffer.append(readLine);                stringBuffer.append("\n");                readLine = bufferedReader.readLine();            }        } catch (Exception e) {            // TODO: handle exception        } finally {            if (bufferedReader != null) {                try {                    bufferedReader.close();                } catch (IOException e) {                    // TODO: handle exception                }            }        }        return stringBuffer.toString();    }

You can change HttpGet to HttpPost / HttpPut / HttpDelete depending on what you need to access.

Cheers.