在应用程序中执行 HTTP 请求 [英] Execute a HTTP request within an app

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

问题描述

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

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 将允许您在应用程序中调用任意 URL:

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

别忘了用 try catch 包裹起来.

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 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆