I'm trying to take data from a mySQL database and my code take it correctly.
The problem is that I have the information in a JsonObjectRequest and out of it, I can't use it. My idea was to use variables to save some of the information I need.
Something like this:
val queue=Volley.newRequestQueue(this)
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET,url,null,
{ response ->
emailGet = response.getString("email")
usernameGet = response.getString("name")
}, { error ->
Toast.makeText(this, error.toString(), Toast.LENGTH_LONG).show()
}
)
queue.add(jsonObjectRequest)
As I said the problem here is that emailGet and usernameGet (variables declared before this code bloc) store the values only inside the JsonObjectRequest, out of it the variables are empty.
Example:
val queue=Volley.newRequestQueue(this)
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET,url,null,
{ response ->
emailGet = response.getString("email")
usernameGet = response.getString("name")
Toast.makeText(this, emailGet, Toast.LENGTH_LONG).show()
}, { error ->
Toast.makeText(this, error.toString(), Toast.LENGTH_LONG).show()
}
)
queue.add(jsonObjectRequest)
Toast.makeText(this, usernameGet, Toast.LENGTH_LONG).show()
Here the Toast will print on the screen only the emailGet content because it's inside the JsonObjectRequest, the Toast that have to print the usernameGet value will not do it.
Looking for information I have found that this problem could be because this function is asynchronous and I found a possible solution in Java that I tried to translate to Kotlin.
val queue=Volley.newRequestQueue(this)
val future : RequestFuture<JSONObject> = RequestFuture.newFuture()
val request = JsonObjectRequest(
Request.Method.GET, url, null, future, future)
queue.add(request)
try{
var response = future.get()
} catch (e : InterruptedException){
} catch (e : ExecutionException){
}
I do not really understand this second code, but it still doesn't working, the response variable is always empty and the program stays in an infinite loop inside that try.
If you want to use the emailGet and usernameGet variables, you should do it within the callback:
val queue = Volley.newRequestQueue(this)
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET, url, null,
{ response ->
emailGet = response.getString("email")
usernameGet = response.getString("name")
// TODO do something with the variables here
}, { error ->
Toast.makeText(this, error.toString(), Toast.LENGTH_LONG).show()
}
)
queue.add(jsonObjectRequest)
If instead, you have a method method doSomething() that runs immediately after the response is received:
fun doSomething(email:String, username:String){
// TODO do something with the variables here
}
You can replace the TODO comment in the first code snippet with doSomething(emailGet, usernameGet).
The 4th and 5th parameters for JsonObjectRequest are in fact objects of types Response.Listener and Response.ErrorListener. These two listeners are Single Abstract Method interfaces. If expanded it would look like this:
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET, url, null,
object : Response.Listener {
override fun onResponse(response: JSONObject) {
emailGet = response.getString("email")
usernameGet = response.getString("name")
doSomething(emailGet, usernameGet)
}
},
object : Response.ErrorListener {
override fun onErrorResponse(error: VolleyError) {
Toast.makeText(this, error.toString(), Toast.LENGTH_LONG).show()
}
}
)
The lambda syntax you were using was a short hand of SAM interfaces.
The simplest way is using #ttyip's answer but you could also use live data and observing it! You're calling an asynchronous method and there's going to be some delay(network API calling and this delay depends on user's internet connection etc) So First you'll need to add jetPack's lifeCycle components inside your project:
dependencies {
def lifecycle_version = "2.4.0-alpha02"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
// Annotation processor
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
// alternately - if using Java8, use the following instead of lifecycle-compiler
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
}
After syncing your project, define global inside your activity/fragment:
val email: MutableLiveData<String> = MutableLiveData<String>()
val username: MutableLiveData<String> = MutableLiveData<String>()
And inside your response:
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET,url,null,
{ response ->
val emailGet = response.getString("email")
val usernameGet = response.getString("name")
email.postValue(emailGet)
username.postValue(usernameGet)
}, { error ->
Toast.makeText(this, error.toString(), Toast.LENGTH_LONG).show()
}
)
And somewhere inside your activity just observe your livedata:
email.observe(this, Observer { string ->
// Do some work
})
username.observe(this, Observer { string ->
// Do some work
})
Related
Before in other random languages I always returned values from functions and I was so surprised now when I try do like below but got error:
fun getChannels(): List<TblChannel> {
val stringRequest = JsonObjectRequest(
Request.Method.GET, "$baseUrl/api/json/channel_list.json",
null,
{ response ->
try{
val gson = Gson()
val token = TypeToken.getParameterized(ArrayList::class.java,TblChannel::class.java).type
val channels1:JSONArray = response.getJSONArray("groups").getJSONObject(0).getJSONArray("channels")
//got "return isn't allowed here" error
return gson.fromJson(channels1.toString(),token)
} catch (e:Exception){
Log.e(tag,"DkPrintError on getChannels: $e")
}
},
{ error ->
Log.e(tag, "DkPrintError on getChannels: $error")
})
requestQueue.add(stringRequest)
}
How can I convert response body to my class and return them?
This isn't really a kotlin problem, we do have functions that return values, however you cannot return a value from asynch function (which is the case here):
If you perform some calculation asynchronously, you cannot directly return the value, since you don't know if the calculation is finished yet. You could wait it to be finished, but that would make the function synchronous again. Instead, you should work with callbacks
source
what you could do tho (as suggested in the quote), is use callbacks, as shown here
That post will be so helpfull to solve that problem.
In that case I solved the problem with callback method and my code was like below:
fun getChannels(onDataReadyCallback: OnDataReadyCallBack){
val stringRequest = JsonObjectRequest(
Request.Method.GET, "$baseUrl/api/json/channel_list.json",
null,
{ response ->
try{
val gson = Gson()
val token = TypeToken.getParameterized(ArrayList::class.java,TblChannel::class.java).type
val channels1:JSONArray = response.getJSONArray("groups").getJSONObject(0).getJSONArray("channels")
onDataReadyCallback.onDataReady(gson.fromJson(channels1.toString(),token))
} catch (e:Exception){
Log.e(tag,"DkPrintError on getChannels: $e")
}
},
{ error ->
Log.e(tag, "DkPrintError on getChannels: $error")
})
requestQueue.add(stringRequest)
}
and I called that fun like:
private fun getChannels(){
viewModelScope.launch {
channelsLiveData.value=roomRepository.getAllChannels
if (channelsLiveData.value.isNullOrEmpty()){
remoteRepository.getChannels(object :OnDataReadyCallBack{
override fun onDataReady(data: List<TblChannel>) {
viewModelScope.launch {
channelsLiveData.value=data
}
}
})
}
}
}
I can send JSON object request to post data on the server using volley but can't work properly. It always shows a volley failure error. I tried it many times but. I can't know why it happens Kindly share with me the preferable answer Kindly.
private fun login() {
val params = JSONObject()
params.put("email",login_email.text.toString())
params.put("password",login_password.text.toString())
val url = "http://192.168.10.100/fitness_app/api/v1/login"
val queue = Volley.newRequestQueue(this)
val request = object : JsonObjectRequest(
Request.Method.POST,url,params,
Response.Listener<JSONObject> { response ->
val code = response.getInt("code")
if(code==101)
{
Toast.makeText(this,"Successfully login", Toast.LENGTH_SHORT).show()
}
else if(code==102)
{
Toast.makeText(this,"Unauthorized user", Toast.LENGTH_SHORT).show()
}
else if(code==103)
{
Toast.makeText(this,"Email does not exist", Toast.LENGTH_SHORT).show()
}
}, Response.ErrorListener {
Toast.makeText(this,"Something went wrong. Please try later.Volley Error", Toast.LENGTH_SHORT).show()
}
)
{
}
queue.add(request)
}
The code given above is exactly write . I just forget to make httptrafficlibrary true in manifest that is false by default.
I'm creating a login for my application.
I am currently stuck in posting problems to my API
This is my API that which is made to support login.
{
success: false,
message: "Please provide complete and accurate information.",
data: [ ]
}
fun loginUrlSuccess(urlApi : String) {
Log.d("login", urlApi)
authSignin_cgi = gson.fromJson(urlApi, DtoProfile::class.java)
loginsSuccess = authSignin_cgi.success
val queue = Volley.newRequestQueue(context)
val stringReq = object : StringRequest(Request.Method.POST,urlApi,Response.Listener<String>{ response ->
Log.w("response",response)
Toast.makeText(context,"Loging success..",Toast.LENGTH_SHORT).show()
if (loginsSuccess){
Toast.makeText(context,authSignin_cgi.message,Toast.LENGTH_LONG).show()
} else {
Toast.makeText(context,authSignin_cgi.message,Toast.LENGTH_LONG).show()
}
},Response.ErrorListener { error ->
Log.w("error", error.toString())
Toast.makeText(context, "error..$error",Toast.LENGTH_SHORT).show()
}){
override fun getParams(): MutableMap<String, String> {
val param = HashMap<String, String>()
val userEmail = textEmail.text.toString().trim()
val userPassword = textPassword.text.toString().trim()
param["useremail"] = userEmail
param["userpassword"] = userPassword
return param
}
}
queue.add(stringReq)
}
I get an error from the Logcat screen.
So what do I have to do?
04-04 15:31:43.614 8365-8699/com.example.atimeonlin5 E/Volley: [700] NetworkDispatcher.processRequest: Unhandled exception java.lang.RuntimeException: Bad URL {"success":false,"message":"โปรดระบุข้อมูลให้ถูกต้องครบถ้วน","data":[]}
java.lang.RuntimeException: Bad URL {"success":false,"message":"โปรดระบุข้อมูลให้ถูกต้องครบถ้วน","data":[]}
All right , you should try to construct an Url object instead of type String !
You should use an url (like "http://www.google.com"), not a random string. Your urlApi is not url.
Example from doc:
val textView = findViewById<TextView>(R.id.text)
// ...
// Instantiate the RequestQueue.
val queue = Volley.newRequestQueue(this)
val url = "http://www.google.com"
// Request a string response from the provided URL.
val stringRequest = StringRequest(Request.Method.GET, url,
Response.Listener<String> { response ->
// Display the first 500 characters of the response string.
textView.text = "Response is: ${response.substring(0, 500)}"
},
Response.ErrorListener { textView.text = "That didn't work!" })
// Add the request to the RequestQueue.
queue.add(stringRequest)
This is the code that is crashing:
val url = "https://chotawhatsapp.firebaseio.com/users.json"
val res = object : Response.Listener<String> {
//Toast.makeText(this#Main2Activity,"show",Toast.LENGTH_LONG).show()
#Override
override fun onResponse(s: String) {
Toast.makeText(this#Main2Activity,"show",Toast.LENGTH_LONG).show()
doOnSuccess(s)
//the function OnResponse is shown never used in the program while toast is properly executable
}
}
val eros = object :Response.ErrorListener {
#Override
override fun onErrorResponse(volleyError: VolleyError) {
Toast.makeText(this#Main2Activity,"volley error - $volleyError",Toast.LENGTH_LONG).show()
}
}
// request = StringRequest(Request.Method.GET,url,)
val request = StringRequest(Request.Method.GET, url, res,eros)
val rQueue = Volley.newRequestQueue(this#Main2Activity)
rQueue.add(request)
There is nothing in logcat for the crash.
The toast messages in the code are not shown.
This code is working fine for me. Only minor differences:
fun f() {
val url = "https://www.google.co.uk/"
val res = object :Response.Listener<String>{
override fun onResponse(response: String?) {
Toast.makeText(this#MainActivity,"no err: $response", Toast.LENGTH_LONG).show()
}
}
val eros = object :Response.ErrorListener {
#Override
override fun onErrorResponse(volleyError: VolleyError){
Toast.makeText(this#MainActivity,"volley error - $volleyError", Toast.LENGTH_LONG).show()
}
}
// request = StringRequest(Request.Method.GET,url,)
val request = StringRequest(Request.Method.GET, url, res,eros)
val rQueue = Volley.newRequestQueue(this#MainActivity)
rQueue.add(request)
}
Gradle:
implementation 'com.android.volley:volley:1.1.1'
Manifest:
<uses-permission android:name="android.permission.INTERNET" />
Project creation procedure:
File -> New Project -> [Tick box: Include Kotlin Support]
I mention this procedure because I've often been less than lucky when converting existing Java apps to Kotlin.
I could not remember if Volley posted its results back to the main thread or a worker thread. But both onResponse and onErrorResponse are called on the main thread so Toast.makeText is safe.
i am trying Android App to connect the Mysql through API call. I am following this article to create the app.
https://www.simplifiedcoding.net/volley-with-kotlin/
But I have populate the mysql JSON result into 2 textView android objects. I am getting error while compile the String Request.
type mismatch: Expected string, Found: int, and another error which are attached here.
StringRequest Function error
Type mismatch error
My code is here. I am new to this Kotlin, I couldn't figure out upto my level. where is the problem caused.. How do I resolve this?
//MainActivity.kt
val requestQueue = Volley.newRequestQueue(this#MainActivity)
downtxtvalue = findViewById<TextView>(R.id.downtxt) as TextView
uploadtxtvalue = findViewById<TextView>(R.id.uptxt) as TextView
//findViewById<Button>(R.id.btnGetspeed).setOnContextClickListener(getSpeed()){
val stringRequest = StringRequest(Request.Method.GET,URL_GET_ARTIST;
Response.Listener<String> { s ->
try {
val obj = JSONObject(s)
if (!obj.getBoolean("error")) {
val array = obj.getJSONArray("internet")
for (i in 0..array.length() - 1) {
val objValue = array.getJSONObject(i)
objValue.getInt("download_value")
objValue.getInt("upload_value")
}
}
} catch (e: JSONException) {
e.printStackTrace()
}
},
Response.ErrorListener { volleyError -> Toast.makeText(applicationContext, volleyError.message, Toast.LENGTH_LONG).show() }
requestQueue.add(stringRequest)
} }