1
votes

I try to send different types of post parameters in api but it did not work correctly. Any idea why?

Here is my code.

Firstly I wrap my parameters in a simple container and pass that as a parameter to AsyncTask, like this:

class MyParams {
    var paramName: String = ""
    var paramaddress: String = ""
    var paramlatitude: Float = 0.toFloat()
    var paramlongitude: Float = 0.toFloat()

    constructor(funName: String, funAddress: String, funLatitude: Float, funLongitude: Float) {
        this.paramName = funName
        this.paramaddress = funAddress
        this.paramlatitude = funLatitude
        this.paramlongitude = funLongitude
    }
}

class myClass : AsyncTask<MyParams, Void, String>() {

    override fun onPreExecute() {
        super.onPreExecute()
    }

    override fun onPostExecute(httpResponseMsg: String) {
        super.onPostExecute(httpResponseMsg)
        ...
    }

    override fun doInBackground(vararg params: MyParams): String {
        var name = params[0].paramName
        var address = params[0].paramaddress
        var latuitude = params[0].paramlatitude
        var longitude  = params[0].paramlongitude
        hashMap["name"] = name
        hashMap["address"] = address
        hashMap["latuitude"] = latuitude
        hashMap["longitude"] = longitude

        finalResult = httpParse.postRequest(hashMap, HttpURL)

        println("responseeeeeeeee-----="+finalResult)
        return finalResult
    }


}

val params = MyParams(funName,funAddress,funLatitude,funLongitude)
val myTask = myClass()
myTask.execute(params)

But in this line hashMap["name"] = name I got the following error:

Type inference failed: Cannot infer type parameter V in inline operator fun MutableMap.set(key: K, value: V): Unit None of the following substitutions receiver: MutableMap arguments: (String,Params) receiver: MutableMap arguments: (String,String) can be applied to receiver: HashMap arguments: (String,String)

2
used retrofit 2.0 for ws calling it is easy. - Android Team
how is your hashMap defined? - user2340612
var hashMap = java.util.HashMap<String, MyParams>() - Pallavi Sharma

2 Answers

1
votes

As you said, you declared hashMap as a variable of type HashMap<String, MyParams>, while you're trying to put a String, and String "is not" MyParams.

To solve the issue, you should either change MyParams to String (i.e., val hashMap = HashMap<String, String>(), which I guess is what you really want), or insert the correct value inside the map (i.e., hashMap["name"] = params[0]).

By the way, var paramlatitude: Float = 0.toFloat() could be simplified to var paramLatitude = 0F

0
votes

var name = params[0].paramName is a String

var address = params[0].paramaddress is a String

So when you do :

hashMap["name"] = name
hashMap["address"] = address

using a Hashmap like this var hashMap = java.util.HashMap<String, MyParams>() it suppose you have to make an HashMap like HashMap and not HashMap.

Change the type of your Hashmap or send a type MyParams in the values of your Hashmap.