0

first i'm german, so sorry for my bad english ;) Im coding an app for my school which gives the students a notification if a lesson is cancelled. My school updates the substitute plan on the internet. It's a .htm file. (http://www.dbg-filderstadt.de/fileadmin/dateien/Dokumente/w00000.htm)

So the point is that i'm new in Android/Java coding and i don't know how to get the information/source code of the website saved in a String.

Could you give me an example code how to do that?

mky
  • 11
  • 1
  • Are you going to send the whole file as a notification? Couldn't you notify them that there has been a change and that's where to look? – DanielM Apr 21 '15 at 12:01
  • you want to display the url content ?. open the url in an browser. this link will help you http://stackoverflow.com/questions/2201917/how-can-i-open-a-url-in-androids-web-browser-from-my-application – Rince Thomas Apr 21 '15 at 12:01
  • http://stackoverflow.com/questions/11338486/how-to-save-html-page-file-in-applications-database-in-android something like that? – BooNonMooN Apr 21 '15 at 12:02
  • well, if you look at the page, you can see there tables. in those tables is the date, lesson, class and which room. and now i want to save those informations in variables/array. But how can i get those data? – mky Apr 21 '15 at 14:02

2 Answers2

0

The one way is to follow the android documentation for sending GET requests and to use the Volley library with the following code:

// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String pageContent = "";
String url ="http://www.dbg-filderstadt.de/fileadmin/dateien/Dokumente/w00000.htm";

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
        new Response.Listener<String>() {
@Override
public void onResponse(String response) {
    // Display the first 500 characters of the response string.
    pageContent = response;
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
    //do something
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);

In this case, when your request is successful (become response code 200 Ok from the server - no errors), the system runs a method called onResponse and you become a string (named response), which is exactly the content of the page you made request to. So you could save it to your variable and use it later (for example I called this variable pageContent).

I attach you a link to the android documentation, too : https://developer.android.com/training/volley/simple.html

Do not forget to add in your AndroidManifest.xml the permissions for requesting the net: <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> and the following dependency for the Volley library to your build.gradle file : compile 'com.mcxiaoke.volley:library:1.0.+'

Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
0

If you want to simply make an HTTP connection to download a HTML file, you could for example use http://developer.android.com/reference/java/net/HttpURLConnection.html. You would then have to read the input stream from it and convert it to a string, and then probably also parse the HTML by some means.

However, note that having an app reading a HTML resource off the internet isn't really the normal or preferable way to do it, ideally you would want your information to be available through some kind of API. By downloading and parsing HTML your app becomes dependent on the HTML resource not changing and breaking your parsing code etc. This is really fragile.

JHH
  • 8,567
  • 8
  • 47
  • 91