Execute a HTTP request within an app

孤街浪徒 提交于 2019-12-13 04:18:36

问题


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


回答1:


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.

edit:

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


来源:https://stackoverflow.com/questions/14947470/execute-a-http-request-within-an-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!