-1

here is url ... https://api.thingspeak.com/update?api_key=R3LQP7IXIXEQ1OIV&field1=0

Actually..I don't want to get the content of url. I just want to update the value of field1 to 1 and 0. If I just type this url in any broswer,my thingspeak data will be update.All I need to do is to write code like typing in any broswer of this url. In that way I think I can turn on and off my led through android application for my IOT project.I think all I need to do is to make connection between apk and thingspeak from this url.I am new to android studio.I have tried many ways.Help me please. Thanks a lot.

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30

1 Answers1

0

You can simply use a HTTP GET method (that is what your browser does). To answer your question:

try {
  // create the HttpURLConnection
  url = new URL(yourUrl);
  HttpURLConnection connection = (HttpURLConnection) url.openConnection();

  // just want to do an HTTP GET here like your browser would
  connection.setRequestMethod("GET");

  // give it 15 seconds to respond
  connection.setReadTimeout(15*1000);
  connection.connect();
} catch (Exception e) {
  e.printStackTrace();
  throw e;
} finally {
  connection.disconnect();
}

There are better (cleaner) ways to do his, but this should answers your question.

PS: Be aware that in Android you will not be able to do this on the main thread! You should do this in a AsyncTask, AsyncTaskLoader, ...

Allinone51
  • 624
  • 1
  • 7
  • 19