2

Can someone help me to solve this ? I'm trying to send data from android to MySQL using code below but I get error long cannot converted to String. I can find plenty of solution related to no suitable method found .put(String) but not in my case, so I decided to seek help from here. Any help would be appreciated.

 public void addWorkForce(final String subcontractors, final String noPeople, final String noHours,final long twf)
    {
        class AddWorkForce extends AsyncTask<String, Void, String> {
            ProgressDialog loading;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(WorkDetailsTable.this, "Please Wait",null, true, true);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
            }

            @Override
            protected String doInBackground(String... params) {

                HashMap<String, String> data = new HashMap<String,String>();
                data.put(Config.KEY_SUBCONTRACTORS,subcontractors);
                data.put(Config.KEY_NUMBERPERSONS,noPeople);
                data.put(Config.KEY_NUMBERHOURS,noHours);
                data.put(Config.KEY_TWF,twf);
                RequestHandler rh=new RequestHandler();
                String result = rh.sendPostRequest(Config.ADD_WORKFORCE,data);
                return  result;
            }
        }

        AddWorkForce ru = new AddWorkForce();
        ru.execute(subcontractors,noPeople,noHours,twf);
    }

Error

Error:(281, 21) error: no suitable method found for put(String,long)
method AbstractMap.put(String,String) is not applicable
(argument mismatch; long cannot be converted to String)
method HashMap.put(String,String) is not applicable
(argument mismatch; long cannot be converted to String)
  • 1
    you are using String where Long is expected. http://stackoverflow.com/questions/22079429/error-no-suitable-method-found-for-putstring-int – Ajinkya Dec 29 '15 at 07:35
  • dear@Seng u are putting long value in string use data.put(Config.KEY_TWF,Strig.valueof(twf)); like this, it will work for u – Kapil Rajput Dec 29 '15 at 07:41

3 Answers3

1

Use

data.put(Config.KEY_TWF,String.valueOf(twf));
Ajinkya
  • 22,324
  • 33
  • 110
  • 161
Vineet Kasat
  • 994
  • 7
  • 14
1

By passing the correct type of parameters. Your class only has that method with two Strings as a parameter, while you try to call it with a String and a long.

Either change the method in the original type to take a long instead (which is a bad idea, since you might break other code doing so.

Or, add a new method that takes a String and a long as parameters.

Or: call it like this:

Instead of

long l = 2L;
callMethod("String", l);

do this

callMethod("String", String.valueOf(l));
Stultuske
  • 9,296
  • 1
  • 25
  • 37
1

Your HashMap is <String, String> means it awaits for String key and String value. You can change the HashMap to <String, Long> or convert your long value to String with method:

String.valueOf(long);
Evgeniy Mishustin
  • 3,343
  • 3
  • 42
  • 81