在Android Studio中打开URL而不打开浏览器 [英] Hit a URL without opening browser in Android Studio
问题描述
我看过很多地方,尝试了很多东西,但似乎没有任何办法对我有用。任何人都可以在这里帮助我。我有一个想要在不打开手机浏览器的情况下点击的简单网址。我单击一个按钮,该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("http://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("http://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中使用代码:
http://loopj.com/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: http://loopj.com/android-async-http/
也可以使用其他一些库,例如Apache Http客户端:
https://hc.apache.org/
It is also possible to use some other library such as Apache Http client as well: https://hc.apache.org/
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而不打开浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!