Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- val username = prefs.getString(context?.getString(R.string.PREFS_MOODLE_LOGIN), "")
- val password = prefs.getString(context?.getString(R.string.PREFS_MOODLE_PASSWORD), "")
- val urlString = "http://35.246.253.130:8000/moodleCoursesParser/$username/$password"
- val url = URL(urlString)
- val urlConnection = url.openConnection() as HttpURLConnection
- return try{
- val inputStream = BufferedInputStream(urlConnection.inputStream)
- val result = String(ByteStreams.toByteArray(inputStream), Charsets.UTF_8)
- urlConnection.disconnect()
- val mainObject = JSONObject(result)
- val coursesArray = mainObject.getJSONArray("courses")
- val courseList = mutableListOf<Course>()
- for (i in 0..(coursesArray.length() - 1)) {
- val course = coursesArray.getJSONObject(i)
- courseList.add(Course(course.getInt("id"), course.getString("name"), ""))
- }
- courseList
- }catch(e: UnknownHostException){
- emptyList()
- }catch(e: Exception){
- Log.v("USOS_COURSES_EXCEPTION", e.printStackTrace().toString())
- emptyList()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement