0

How to send a simple http command without opening the browser??

public void addListenerOnButton() {

    button = (Button) findViewById(R.id.button1);

    button.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {

        Intent browserIntent = 
                    new Intent(Intent.ACTION_VIEW, Uri.parse("http://192.168.1.95:8080/json.htm?type=command&param=switchlight&idx=2&switchcmd=Off&level=0"));
        startActivity(browserIntent);
    }
});
Purushotham
  • 3,770
  • 29
  • 41

3 Answers3

0

I think, I know what you want to do. A simple example here, could be helpful.

Rohan Shah
  • 235
  • 2
  • 11
0

HTTPClient is what you are looking for. Make sure to use it in a background thread, e.g. in an AsyncTask.

A tutorial like this will get you started: http://hmkcode.com/android-internet-connection-using-http-get-httpclient/

FD_
  • 12,947
  • 4
  • 35
  • 62
0

The same question Make an HTTP request with android

Simply, here is the code ( http://www.androidhive.info/2011/10/android-making-http-requests/ )

 // Creating HTTP client
        HttpClient httpClient = new DefaultHttpClient();
        // Creating HTTP Post
        HttpPost httpPost = new HttpPost(
                "http://www.example.com/login");

        // Building post parameters
        // key and value pair
        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
        nameValuePair.add(new BasicNameValuePair("email", "user@gmail.com"));
        nameValuePair.add(new BasicNameValuePair("message",
                "Hi, trying Android HTTP post!"));

        // Url Encoding the POST parameters
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
        } catch (UnsupportedEncodingException e) {
            // writing error to Log
            e.printStackTrace();
        }

        // Making HTTP Request
        try {
            HttpResponse response = httpClient.execute(httpPost);

            // writing response to log
            Log.d("Http Response:", response.toString());
        } catch (ClientProtocolException e) {
            // writing exception to log
            e.printStackTrace();
        } catch (IOException e) {
            // writing exception to log
            e.printStackTrace();

        }
Community
  • 1
  • 1
feelinglucky
  • 186
  • 2
  • 5