adds list functionality
- adds rest connection with gets a JSON Object with a "messages" list inside it. - test case, which pushes a new message and gets an entire list. checking that the first element is "hello" and the last element to be the new element just pushed
This commit is contained in:
@ -6,6 +6,7 @@ import com.android.volley.Request
|
||||
import com.android.volley.RequestQueue
|
||||
import com.android.volley.toolbox.*
|
||||
import com.maenle.bump.MainActivity
|
||||
import org.json.JSONArray
|
||||
|
||||
import org.json.JSONObject
|
||||
|
||||
@ -28,14 +29,31 @@ class RestSingleton constructor(context: Context){
|
||||
private const val URL = "http://192.168.68.127:4000/api/"
|
||||
}
|
||||
|
||||
fun peek(sender: String, callback: (String) -> Unit){
|
||||
fun list(sender: String, callback: (JSONObject) -> Unit){
|
||||
val url = URL + "list/"
|
||||
val data = JSONObject()
|
||||
data.put("sender", sender)
|
||||
data.put("minutes", (60*24*365*100).toString())
|
||||
val jsonRequest = JsonObjectRequest(Request.Method.POST, url,
|
||||
data,
|
||||
{ response ->
|
||||
run {
|
||||
callback(response)
|
||||
}
|
||||
} ,
|
||||
{callback(JSONObject())})
|
||||
|
||||
requestQueue.add(jsonRequest)
|
||||
}
|
||||
|
||||
fun peek(sender: String, callback: (JSONObject) -> Unit){
|
||||
val url = URL + "peek/"
|
||||
val data = JSONObject()
|
||||
data.put("sender", sender)
|
||||
val jsonRequest = JsonObjectRequest(Request.Method.POST, url,
|
||||
data,
|
||||
{ response -> callback(response.toString()) },
|
||||
{callback("")})
|
||||
{ response -> callback(JSONObject(response.toString())) },
|
||||
{callback(JSONObject())})
|
||||
|
||||
requestQueue.add(jsonRequest)
|
||||
}
|
||||
@ -54,14 +72,14 @@ class RestSingleton constructor(context: Context){
|
||||
requestQueue.add(stringRequest)
|
||||
}
|
||||
|
||||
fun pop(sender: String,callback: (String) -> Unit){
|
||||
fun pop(sender: String,callback: (JSONObject) -> Unit){
|
||||
val url = URL + "pop/"
|
||||
val data = JSONObject()
|
||||
data.put("sender", sender)
|
||||
val stringRequest = JsonObjectRequest(Request.Method.POST, url,
|
||||
data,
|
||||
{ response -> callback(response.toString()) },
|
||||
{callback("")})
|
||||
{ response -> callback(JSONObject(response.toString())) },
|
||||
{callback(JSONObject())})
|
||||
|
||||
requestQueue.add(stringRequest)
|
||||
}
|
||||
@ -72,7 +90,4 @@ class RestSingleton constructor(context: Context){
|
||||
// Activity or BroadcastReceiver if someone passes one in.
|
||||
Volley.newRequestQueue(context.applicationContext)
|
||||
}
|
||||
fun <T> addToRequestQueue(req: Request<T>) {
|
||||
requestQueue.add(req)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user