0
private int ID=0;
.
.
Integer field2=0;
field2 = Integer.parseInt(item.get("credentialID").toString());
ID=field2.intValue();
UName.setText(Integer.toString(ID));

Above: field2 gets value '9' and UName shows value '9' on textfield as expected.

HashMap<String,String> paramage= new HashMap<String, String>();
paramage.put("credential", Integer.toString(ID));

Now when i call method using paramage i get no result (means comparison of ID in method resulted false).

However, if I do this and call method now,it works flawlessly (but I can't provide static output to method ,it should be taken from user)

HashMap<String,String> paramage= new HashMap<String, String>();
paramage.put("credential", "9" );

What's the problem? How to Solve it?

Btw I am calling a method on kumulos and I am programming for android.

EDIT: My exact code as requested.

package lcukerd.com.logintest;

import android.os.Bundle;
import android.support.annotation.BoolRes;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Application;
import android.widget.Button;
import android.widget.EditText;

import com.kumulos.android.Kumulos;
import com.kumulos.android.ResponseHandler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;

public class MainActivity extends AppCompatActivity {

    private int ID=0;
    private EditText UName,UPass,UAge;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Kumulos.initWithAPIKeyAndSecretKey("removed", "removed", this);
        UName =(EditText) findViewById(R.id.name);
        UPass = (EditText) findViewById(R.id.password);
        UAge =  (EditText) findViewById(R.id.age);
        Button login = (Button) findViewById(R.id.login);

        login.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {

                String username = UName.getText().toString();
                String password = UPass.getText().toString();
                LinkedHashMap<String, String> params = new LinkedHashMap<String, String>();
                params.put("accountName", username);
                params.put("password",password);
                    Kumulos.call("login", params, new ResponseHandler() {
                        @Override
                        public void didCompleteWithResult(Object result) {

                            Integer field2=0;
                            ArrayList<LinkedHashMap<String, Object>> objects = (ArrayList<LinkedHashMap<String,Object>>) result;
                            LinkedHashMap<String, Object> item= objects.get(0);
                            field2 = Integer.parseInt(item.get("credentialID").toString());
                            ID=field2.intValue();
                            UName.setText(Integer.toString(ID));
                        }
                    });

                params.clear();
                HashMap<String,String> paramage= new HashMap<String, String>();
                paramage.put("credential",  Integer.toString(ID));     //up here
                    Kumulos.call("getage", paramage, new ResponseHandler() {
                        @Override
                        public void didCompleteWithResult(Object result) {
                            Integer field2=0;
                            ArrayList<LinkedHashMap<String, Object>> objects = (ArrayList<LinkedHashMap<String,Object>>) result;
                            LinkedHashMap<String, Object> item= objects.get(0);
                            //Boolean check = item.containsKey("age");
                            field2 = Integer.parseInt(item.get("age").toString());
                            int age=field2.intValue();
                            UAge.setText(Integer.toString(age));
                        }
                    });
                }
        });
        Button signup = (Button) findViewById(R.id.signup);
        signup.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {

                String username = UName.getText().toString();
                String password = UPass.getText().toString();
                HashMap<String, String> params = new HashMap<String, String>();
                params.put(username, password);
                try {
                    Kumulos.call("signup", params, new ResponseHandler() {
                        @Override
                        public void didCompleteWithResult(Object result) {

                            ID = (int) result;
                        }
                    });
                    UName.setText("");
                    UPass.setText("");
                    params.put(UAge.getText().toString(), Integer.toString(ID));
                    Kumulos.call("setAge", params, new ResponseHandler() {
                        @Override
                        public void didCompleteWithResult(Object result) {


                        }
                    });
                    UAge.setText(Integer.toString(ID));

                }
                catch (Exception e)
                {
                    UAge.setText("5");
                    e.printStackTrace();
                }

            }
        });
    }

}

Code after signup Button declaration is not tested and edited dont look there.

lcukerd
  • 83
  • 1
  • 10
  • check your keys.. `credential` and `credentialID` . are you accessing the same data set with that keys. – SRB Bans Nov 29 '16 at 09:05
  • 1
    You already have `item.get("credentialID").toString()` as a `String`. Why are you converting it to an integer and back again? – user207421 Nov 29 '16 at 09:06
  • I find a bit strange how you get a String to convert into Integer to reconvert it to String. Is it necessary ? Plus, provide a [mcve] please. – AxelH Nov 29 '16 at 09:08
  • Why is `field2` an `Integer`? `parseInt()` returns an `int`, so auto-boxing to `Integer`, just to manually unbox using `intValue()` is a total waste. – Andreas Nov 29 '16 at 09:08
  • I checked with your existing code, it's already working. Add your exact code how are you doing with ID's? – Ready Android Nov 29 '16 at 09:10
  • @EJP i am checking the value of field2 by printing in UName. That line is only for checking, i will remove that line once program starts working fine – lcukerd Nov 29 '16 at 09:10
  • @sourabhbans accessing different tables – lcukerd Nov 29 '16 at 09:12
  • Check once your output just after input to the paramage HashMap Log.e("paramage", paramage.get("credential"));. Is it correct or not whatever you had insert in HashMap? – Ready Android Nov 29 '16 at 09:13
  • @ReadyAndroid i initialised Hashmap just before displaying in Uname. It still doesnt work. I have included the full code btw – lcukerd Nov 29 '16 at 09:19
  • `Kumulos.call` is async and you set the `ID` value inside the `ResponseHandler`. Maybe `paramage.put("credential", Integer.toString(ID))` is executed before `ID=field2.intValue()`. Can you check it? – Tobías Nov 29 '16 at 09:22
  • Why is this question voted down? It seems a good, well formulated question, and it has generated discussion. The OP might be doing some strange things in Java, but that doesnt make this a bad question. – CocoNess Nov 29 '16 at 09:22
  • This is due to your webservicer response callback time issue. Check readyandroid answer. – Ready Android Nov 29 '16 at 09:25
  • @Andreas it was leftover from previous things i tried. Currently my priority is to make it running not to make it compact and efficient I will do editing later.. – lcukerd Nov 29 '16 at 09:25
  • @lcukerd You are doing more than just 'checking'. You are engaging in an unnecessary data conversion round-trip. – user207421 Nov 29 '16 at 09:25
  • @Tobias you were right – lcukerd Nov 29 '16 at 09:32

1 Answers1

0

This all is happening because your webservice response is coming later then your code execution for paramage Hashmap input process. When you are adding static value then it is working so in this case you have to add data after webservice response like below.

HashMap<String,String> paramage= new HashMap<String, String>();

Kumulos.call("login", params, new ResponseHandler() {
            @Override
            public void didCompleteWithResult(Object result) {

                Integer field2 = 0;
                ArrayList<LinkedHashMap<String, Object>> objects = (ArrayList<LinkedHashMap<String, Object>>) result;
                LinkedHashMap<String, Object> item = objects.get(0);
                field2 = Integer.parseInt(item.get("credentialID").toString());
                ID = field2.intValue();
                UName.setText(Integer.toString(ID));

                paramage.put("credential", Integer.toString(ID));     //up here

                //As you are making web service call for "getage" so I put this code here. You can make a callback to write this code outside for "getage" webservice call.
                Kumulos.call("getage", paramage, new ResponseHandler() {
                    @Override
                    public void didCompleteWithResult(Object result) {
                        Integer field2 = 0;
                        ArrayList<LinkedHashMap<String, Object>> objects = (ArrayList<LinkedHashMap<String, Object>>) result;
                        LinkedHashMap<String, Object> item = objects.get(0);
                        //Boolean check = item.containsKey("age");
                        field2 = Integer.parseInt(item.get("age").toString());
                        int age = field2.intValue();
                        UAge.setText(Integer.toString(age));
                    }
                });
            }
        });
Ready Android
  • 3,529
  • 2
  • 26
  • 40
  • Thanks a lot it worked. Didn't knew android multi threads on its own in such cases – lcukerd Nov 29 '16 at 09:31
  • Yes, Webservice call is doing on a background thread and you are doing some other works on UI thread with other background threads, that is making mismatches. – Ready Android Nov 29 '16 at 09:33