0

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.

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

Thankyou

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();
    }
}
Patrick Trentin
  • 7,126
  • 3
  • 23
  • 40
Sarthak Jain
  • 31
  • 1
  • 5
  • Do you have `INTERNET` permission in the manifest? Are you calling this method inside an `AyncTask` or `Thread`? – K Neeraj Lal Feb 17 '17 at 10:02
  • **nothing to seem to work** - What do you mean by that? Whats the error/exception ? – Prerak Sola Feb 17 '17 at 10:05
  • Yes i have added INTERNET permission and i am just pressing a button, and added onClick in that and referred to this function. I hope i am implementing it correctly. – Sarthak Jain Feb 18 '17 at 09:27
  • There are no errors, the apk is generating correctly, but when i am pressing the button nothing is happening on the mobile, like no response at all. – Sarthak Jain Feb 18 '17 at 09:28

3 Answers3

3

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

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/

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 ...
Mehdi
  • 186
  • 2
  • 8
  • I believe for this i would need to add AsyncHttpClient dependencies. Ummm i have not wrapped my head around android studio that much. Would you please be willing to help me here in how to do it? – Sarthak Jain Feb 18 '17 at 09:56
  • Thankyou, it solved my purpose. I achieved what i wanted to in this question, thankyou all for the help. – Sarthak Jain Feb 18 '17 at 10:36
  • No problem. I added more explanation on how to use the code as well. – Mehdi Feb 20 '17 at 06:38
1

Use this method to open url with in your application

You can call this url like your api calling, it calls url without any display. Or make an api(web service) and hit from the application and at api end call your url.

chetan prajapat
  • 311
  • 1
  • 9
  • Ummm, can you please redirect me to any link from where i can read about what you said, or maybe even write a bit of code for the same so that i can understand it properly. Thankyou – Sarthak Jain Feb 18 '17 at 09:25
0

First make a web view and then make it invisible so that it wont be displayed. Then use this web view to load the url the it will be ok and the external browser will not be executed.

val myWebView: WebView = findViewById(R.id.WebView)

use below line for the executing the url then it will be fine.

myWebView.loadUrl("http://192.168.100.3/on")
myWebView.loadUrl("http://192.168.100.3/off")

The demo program if we use toggle switch then it will be.

val myWebView: WebView = findViewById(R.id.WebView)

        val switch1: ToggleButton = findViewById(R.id.toggleButton)
        switch1.setOnCheckedChangeListener { _, isChecked ->
            Toast.makeText(this, if(isChecked) " ON" else " OFF", Toast.LENGTH_SHORT).show()

            if(isChecked){
               
                myWebView.loadUrl("http://192.168.100.3/on")

            }
            else{
               
                myWebView.loadUrl("http://192.168.100.3/off")

            }
}
CJK
  • 1
  • 1