2014-02-19 42 views
0

如何在不打开浏览器的情况下发送简单的http命令?Android发送简单的HTTP命令

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); 
    } 
}); 

回答

0

我想,我知道你想要做什么。一个简单的例子here,可能会有所帮助。

+0

哦对了,不要忘记使用的AsyncTask ... –

0

同样的问题Make an HTTP request with android

简单地说,这里是代码(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", "[email protected]")); 
     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(); 

     }