1

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)

3
  • used retrofit 2.0 for ws calling it is easy. Commented Jun 6, 2018 at 6:36
  • 1
    how is your hashMap defined? Commented Jun 6, 2018 at 6:43
  • var hashMap = java.util.HashMap<String, MyParams>() Commented Jun 6, 2018 at 7:26

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

I did that but then it gives error in this line "hashMap["latuitude"] = latuitude" it says - Type inference failed: Cannot infer type parameter V in inline operator fun <K, V> MutableMap<K, V>.set(key: K, value: V): Unit None of the following substitutions receiver: MutableMap<String, String> arguments: (String,String) receiver: MutableMap<String, Float> arguments: (String,Float) can be applied to receiver: HashMap<String, String> arguments: (String,Float)
What is httpParse's type? From which library is it from? I found another question using the same class (no link to the library, though), and there the postRequest method was accepting a Map<String, String> as a parameter, so it means that you need to convert your latitude and longitude to String, e.g., by calling toString() on them (but it depends on how the server expects input parameters to be formatted)
For latitude and longitude server expects input in Float.
ok, but I guess they should be passed as strings, since it's an HTTP request. Try to call latitude.toString() when adding it to your Map<String, String>, it should work
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.