1

I'm trying to make a simple LED burning application for my IoT project, but I haven't been able to do it for months. What I want to do is press a button on my android application and I want it to go to http://92.168.4.1/?State=i in the background.Please help me :(

 on.setOnClickListener(new View.OnClickListener() {

      @Override public void onClick(View v) {
            String webID = "http://192.168.4.1/?State=i";
            Intent bIntent = new Intent();
            bIntent.setAction(Intent.ACTION_VIEW);
            bIntent.addCategory(Intent.CATEGORY_BROWSABLE);
            bIntent.setData(Uri.parse(webID));
            startActivity(bIntent);
      }
});
lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
Musa Eşin
  • 13
  • 2

1 Answers1

0

The code you wrote above asks the system to find another app that can handle that URL and open it. There are likely no apps on your system that fit all the filters you added so the system does nothing. A better solution would likely be to just have your app make the HTTP request itself.

There are many ways of making an HTTP request directly from your app. The old way of doing it is via the Apache HTTP client as seen on this answer. That would look something like this (note this is likely not working code):

@Override
public void onClick(View v) {
    HttpClient httpclient = new DefaultHttpClient(); // note that this is bundled with Android not Java
    HttpResponse response = httpclient.execute(new HttpGet("http://92.168.4.1/?State=i"));
}

However there are far better ways of doing it now (or so I'm told), a few are linked in that answer and you can find more in the documentation

Note that in order for your app to use the internet you need to add the internet permission to your manifest like so:

<uses-permission android:name="android.permission.INTERNET" />
Menachem Hornbacher
  • 2,080
  • 2
  • 24
  • 36