1

I am trying to get data from an Http request to my own API. Running the request in my browser (swapping the IP with localhost) gets me:

["Herbalism","Mining","Skinning","Alchemy","Blacksmithing","Enchanting","Engineering","Inscription","Jewelcrafting","Leatherworking","Tailoring","Archaeology","Cooking","Fishing"]

I am using the following code, modified from the example provided here: https://www.tutorialspoint.com/how-to-use-volley-library-to-parse-json-in-android-kotlin-app It does not print my "Print anything at all". From what I can tell this does nothing. I have tried many different things including suggestions from here: Can I do a synchronous request with volley? and here: Volley Timeout Error and feel that I am in no way closer to getting this request to work. The firewall permits this traffic. The catch JSONException and Response.ErrorListener are also not putting anything out. I am using plain http, no certs or anything. Ultimately I want to populate a Spinner with this data but for now I can't even get this most basic implementation to work.

package com.wowahapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.*
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.Response
import com.android.volley.toolbox.*
import org.json.JSONException


class AddRecipeActivity : AppCompatActivity() {

    lateinit var searchTextView : TextView
    private var requestQueue : RequestQueue? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_recipe)


        searchTextView = findViewById<TextView>(R.id.searchTextView) as TextView

        getAllProfessions(searchTextView)

    }

    fun getAllProfessions(searchText : TextView) {

        val url = "http://192.168.0.24:49155/allprofessions"
        val request = JsonObjectRequest(Request.Method.GET, url, null, Response.Listener {
                response ->try {
            val jsonArray = response.getJSONArray("name")
            println("Print anything at all!")
            for (i in 0 until jsonArray.length()) {
                println(jsonArray.getJSONObject(i))
            }
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        }, Response.ErrorListener { error -> error.printStackTrace() })
        requestQueue?.add(request)
    }
}
Varsha Kulkarni
  • 1,389
  • 2
  • 13
  • 17
craybobnee
  • 55
  • 7

2 Answers2

1

This document explain the implementation of a VolleyWebService class: http://code.sunnyjohn.in/index.php/2020/12/24/retrieve-data-volley/

You have to instantiate the class, create a new request queue and then add to the request queue.

Sunny John
  • 96
  • 3
  • Hi. Thank you for your suggestion. I'm following the tutorial but I am stuck on the line the Fragment where it says ```private lateinit var recyclerViewAdapter: CuisineRecyclerViewAdapter``` It is telling my 'unresolved reference'. I don't see it anywhere else in the tutorial, and if I follow the IDE's suggestion and let it create an empty CuisineRecyclerViewAdapter the editor stops complaining but I feel that has a small chance of doing what I want. – craybobnee Feb 21 '21 at 22:20
  • 1
    The fragment and CuisineRecyclerViewAdapter is only an example. You can replace it with your own List RecyclerAdapter. You can use VolleyWebService class in full .and use it in your function getAllProfessions to retrieve :he data you require.passing the JsonObjectRequest you created like: ```VolleyWebService.getInstance(context).addToRequestQueue(jsonObjectRequest)``` – Sunny John Feb 22 '21 at 04:42
1

First create custom VolleyWebService class as follows:

class VolleyWebService constructor(context: Context) {
    private var INSTANCE: VolleyWebService? = null
           companion object {
            @Volatile
            private var INSTANCE: VolleyWebService? = null
            fun getInstance(context: Context) =
                INSTANCE ?: synchronized(this) {
                    INSTANCE ?: VolleyWebService(context).also {
                        INSTANCE = it
                    }
                }
        }
    
    val requestQueue: RequestQueue by lazy {
                Volley.newRequestQueue(context.applicationContext)
    }

    fun <T> addToRequestQueue(req: Request<T>) {
        requestQueue.add(req)
    }
}

Then modify your function getAllProfessions like this:

fun getAllProfessions(searchText : TextView) {

        val url = "http://192.168.0.24:49155/allprofessions"
        val request = JsonObjectRequest(Request.Method.GET, url, null, Response.Listener {
                response ->try {
            val jsonArray = response.getJSONArray("name")
            println("Print anything at all!")
            for (i in 0 until jsonArray.length()) {
                println(jsonArray.getJSONObject(i))
            }
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        }, Response.ErrorListener { error -> error.printStackTrace() })
//changes made here
      VolleyWebService.getInstance(context).addToRequestQueue(request)  
    }
}
Sunny John
  • 96
  • 3