在应用程序中执行HTTP请求

joe*_*ab3 0 html android arduino hyperlink

我想知道我是否可以打开它但是没有打开android浏览器,我只需要它访问:(假装这是ip)http; // 91.91.91.91:2228?1,它将触发我的arduino上的动作兆.我试图用这段代码来做到这一点

onclick(Intent websiteIntent = new Intent(Intent.ACTION_VIEW);
    Uri uri = Uri.parse("http;//91.9.91.91:?1");
    websiteIntent.setData(uri);
    startActivity(websiteIntent);)
Run Code Online (Sandbox Code Playgroud)

但我不知道怎么做到这一点

Ljd*_*son 5

HttpClient允许您在应用中调用任意URL:

DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http;//91.9.91.91:?1");
HttpResponse response = client.execute(request);
Run Code Online (Sandbox Code Playgroud)

不要忘记尽量包装尝试.

编辑:

new Thread(){
    public void run(){
        try{
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet("http;//91.9.91.91:?1");
            HttpResponse response = client.execute(request);
        }catch(Exception e){
            // Handle the exception
            e.printStackTrace();
        }
    }
};
Run Code Online (Sandbox Code Playgroud)