在Android Studio中打开URL而不打开浏览器

编程入门 行业动态 更新时间:2024-10-06 18:31:17
本文介绍了在Android Studio中打开URL而不打开浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我看过很多地方,尝试了很多东西,但似乎没有任何办法对我有用。任何人都可以在这里帮助我。我有一个想要在不打开手机浏览器的情况下点击的简单网址。我单击一个按钮,该URL需要被点击,而没有打开浏览器。我不关心该页面上的内容,我只想加载它。对于我的arduino暨以太网屏蔽物联网项目,我基本上需要它,其中我用自己的android手机控制设备。

i have looked at many places, tried a ton of things but nothing to seem to work for me somehow. Can anyone please help me here. I have a simple url that i want to hit without opening the browser in mobile phones. I am clicking a button and that url needs to get hit, without the browser getting opened. I am not concerned with what is on that page i just want to load it. I basically need it for my arduino cum ethernet shield IOT project, wherein i am controlling my appliances with my android phone.

这里是我在做什么的一小段。该apk生成没有错误,但该URL并未以某种方式被点击。

Here is a snippet of what i am doing. The apk is generating without errors but the url is not getting hit somehow.

谢谢

public void led1on(View view){ try { URL u = new URL("172.17.27.173/?2"); HttpURLConnection http = (HttpURLConnection) u.openConnection(); http.connect(); }catch (Exception e){ e.getMessage(); e.printStackTrace(); } }

推荐答案

您可以将其称为网络服务,而对结果不执行任何操作。例如:

You can call it as a web service and do nothing with the results. For example:

AsyncHttpClient client = new AsyncHttpClient(); client.get("172.17.27.173/yourpage", params, new AsyncHttpResponseHandler() { @Override public void onSuccess(String response) { // 200 OK // Do nothing } @Override public void onFailure(int statusCode, Throwable error, String content) { // Response not 200 { // Some troubleshooting, etc. } });

为了了解如何使用以上代码,您可以在这里看一下如何在Android中使用代码: loopj/android-async -http /

In order to see how to use the above code, you can take a look at here which has explained how to use the code for Android: loopj/android-async-http/

也可以使用其他一些库,例如Apache Http客户端: hc.apache/

It is also possible to use some other library such as Apache Http client as well: hc.apache/

try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); myContent = httpEntity.getContent(); } catch ...

更多推荐

在Android Studio中打开URL而不打开浏览器

本文发布于:2023-11-12 21:14:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582540.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不   打开浏览器   Android   Studio   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!