Advertisement
Guest User

Untitled

a guest
Jan 30th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.20 KB | None | 0 0
  1. val username = prefs.getString(context?.getString(R.string.PREFS_MOODLE_LOGIN), "")
  2.         val password = prefs.getString(context?.getString(R.string.PREFS_MOODLE_PASSWORD), "")
  3.         val urlString = "http://35.246.253.130:8000/moodleCoursesParser/$username/$password"
  4.         val url = URL(urlString)
  5.         val urlConnection = url.openConnection() as HttpURLConnection
  6.         return try{
  7.             val inputStream = BufferedInputStream(urlConnection.inputStream)
  8.             val result = String(ByteStreams.toByteArray(inputStream), Charsets.UTF_8)
  9.             urlConnection.disconnect()
  10.             val mainObject = JSONObject(result)
  11.             val coursesArray = mainObject.getJSONArray("courses")
  12.             val courseList = mutableListOf<Course>()
  13.             for (i in 0..(coursesArray.length() - 1)) {
  14.                 val course = coursesArray.getJSONObject(i)
  15.                 courseList.add(Course(course.getInt("id"), course.getString("name"), ""))
  16.             }
  17.             courseList
  18.         }catch(e: UnknownHostException){
  19.             emptyList()
  20.         }catch(e: Exception){
  21.             Log.v("USOS_COURSES_EXCEPTION", e.printStackTrace().toString())
  22.             emptyList()
  23.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement