summaryrefslogtreecommitdiff
path: root/app/src/main/java/uk/co/blatech/blaupload3/util/JSONLoader.kt
blob: 2560849182baf83792066d3c287460f469f4918d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package uk.co.blatech.blaupload3.util

import android.content.Context
import android.util.Log
import android.widget.ListView
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.JsonArrayRequest
import com.android.volley.toolbox.Volley
import org.json.JSONArray
import uk.co.blatech.blaupload3.model.File


class JSONLoader {
    companion object {

        fun loadJsonToList(context: Context?, listView: ListView) {
            val queue = Volley.newRequestQueue(context)
            val url = "http://wupload.of.je/?format=json"
            var fileList = ArrayList<File>()
            val jsonRequest = JsonArrayRequest(
                Request.Method.GET,
                url,
                null,
                Response.Listener<JSONArray> { response ->

                    for (i in 0 until response.length()) {
                        var jsonObj = response.getJSONObject(i)
                        var upload: File = File(filename =jsonObj.getString("filename"), type = jsonObj.getString("type"), modified = jsonObj.getLong("modified"), size = jsonObj.getLong("size"))
                        fileList.add(upload)


                    }
                    if (context != null) {
                        listView.adapter = FileListAdapter(context, fileList)
                    }
                },
                Response.ErrorListener {  Log.e("json error", "narp")})
                queue.add(jsonRequest)

        }

        fun loadJsonToAdapter(context: Context?, adapter: ImageGalleryAdapter) {
            val queue = Volley.newRequestQueue(context)
            val url = "http://wupload.of.je/?format=json"
            var fileList = ArrayList<File>()
            val jsonRequest = JsonArrayRequest(
                Request.Method.GET,
                url,
                null,
                Response.Listener<JSONArray> { response ->

                    for (i in 0 until response.length()) {
                        var jsonObj = response.getJSONObject(i)
                        var upload: File = File(filename =jsonObj.getString("filename"), type = jsonObj.getString("type"), modified = jsonObj.getLong("modified"), size = jsonObj.getLong("size"))
                        fileList.add(upload)


                    }
                        adapter.updateFileList(fileList)
                },
                Response.ErrorListener {  Log.e("json error", "narp")})
            queue.add(jsonRequest)

        }
    }




}