Devyani Kotadiya
Devyani Kotadiya

Reputation: 450

How to use curl in Android?

I want to give OFF command on switch click listener using curl with login authentication. Like http://admin:[email protected]/outlet?4=OFF

I set click listener on switch like this and on click listener i call one method for curl

switch4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getRequest();

            }
        });

Now, getRequest() method like this...

public static String getRequest() {
       StringBuffer stringBuffer = new StringBuffer("");
       BufferedReader bufferedReader = null;
       try {
           HttpClient httpClient = new DefaultHttpClient();
           HttpPost httpPost = new HttpPost();

           URI uri = new URI("http://10.1.1.83/outlet?4=OFF");
           httpPost.setURI(uri);
           httpPost.addHeader(BasicScheme.authenticate(
                   new UsernamePasswordCredentials("admin", "1234"),
                   HTTP.UTF_8, false));

           HttpResponse httpResponse = httpClient.execute(httpPost);
           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();
   }

Upvotes: 3

Views: 6455

Answers (1)

Jay Katira
Jay Katira

Reputation: 181

You can use httpget method with username, password credentials. Here i show you the method.

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://10.1.1.83/outlet?2=OFF");
        httpGet.setURI(uri);
        httpGet.addHeader(BasicScheme.authenticate(
new UsernamePasswordCredentials("admin", "1234"),
                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();
}

use this code in your script, it'll work.

Upvotes: 2

Related Questions