Issue
Im trying to get data from an api, and im running into an error on this line
val queue = Volley.newRequestQueue(this@MainActivity)
The error says "Type mismatch: inferred type is Home but Context! was expected"
I have tried changing "this" to "this@MainActivity", However this just made Main Activity go red and when i hover over it, it says unresolved reference
My whole code is below, thanks for any help in advance
package com.example.temperaturesensor
import android.app.DownloadManager
import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import org.json.JSONArray
import org.json.JSONTokener
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [Home.newInstance] factory method to
* create an instance of this fragment.
*/
class Home : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val queue = Volley.newRequestQueue(this@MainActivity)
val url = "https://io.adafruit.com/api/v2/alex9301/feeds/test-data/data" // Your URL
var urlStuff = ""
val stringRequest = StringRequest(Request.Method.GET, url,
{ response ->
// Display the first 500 characters of the response string.
urlStuff = response
val jsonArray = JSONTokener(urlStuff).nextValue() as JSONArray
for (i in 0 until jsonArray.length()) {
// ID
val id = jsonArray.getJSONObject(i).getString("value") // Get data
Log.i("Val: ", id)
}
},
{ Log.i("b", "That didn't work!") })
queue.add(stringRequest)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false)
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Home.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
Home().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
Solution
package com.example.temperaturesensor
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import org.json.JSONArray
import org.json.JSONTokener
class Home : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_home, container, false)
val queue = Volley.newRequestQueue(view.context)
val url = "https://io.adafruit.com/api/v2/alex9301/feeds/test-data/data" // Your URL
var urlStuff = ""
val stringRequest = StringRequest(Request.Method.GET, url,
{ response ->
// Display the first 500 characters of the response string.
urlStuff = response
val jsonArray = JSONTokener(urlStuff).nextValue() as JSONArray
for (i in 0 until jsonArray.length()) {
// ID
val id = jsonArray.getJSONObject(i).getString("value") // Get data
Log.i("Val: ", id)
}
},
{ Log.i("b", "That didn't work!") })
queue.add(stringRequest)
return view
}
}
Answered By - Meet Bhavsar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.