本文介绍了一个应用程序中执行一个HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果我可以把它打开但未打开Android浏览器,我只需要它来访问:(pretend这是IP)的HTTP; // 91.91.91.91:2228?1,在那里将触发我的Arduino的大型行动。我试图让它正好与此code做到这一点。

I was wondering if I could make it open But NOT open the android browser, I just need it to visit: (pretend this is the ip) http;//91.91.91.91:2228?1, where it will trigger action on my arduino mega. I have tried to get it just to do this with this code

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

但我不知道如何得到它这样做。

but I don't know how to get it to do so

推荐答案

一个HttpClient的可以让你您的应用程序中调用任意网址:

A HttpClient will allow you to call an arbitrary URL within your app:

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

不要忘了在尝试捕捉到,虽然包装

Don't forget to wrap in a try catch though.

编辑:

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

这篇关于一个应用程序中执行一个HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 06:20