0

I have a function to read JSON from a web service:

public String readJSONFeed(String URL, String userName, String password){
...
    List<NameValuePair> params = new ArrayList<NameValuePair>(2);
    params.add(new BasicNameValuePair("userName", userName));
    params.add(new BasicNameValuePair("password", password));
...
}

And I call it from an AsyncTask:

EditText userName = (EditText)findViewById(R.id.txtUserName);
EditText password = (EditText)findViewById(R.id.passWord);

return readJSONFeed("http://service.com/service.php", userName.getText().toString(), password.getText().toString());

My question is how can make the readJsonFeed generic and have it parse an array into name value pairs.

silversunhunter
  • 1,219
  • 2
  • 12
  • 32

1 Answers1

0

You can try using String ... params and then getting the values with params[0], params[1], etc. This will complicate things a bit because you need the name of the param and the value, but you can put the name, value like this when sending the params to the method, always, and make sure the params are always in pairs.

For example:

public String readJSONFeed(String ... params){

    if (params.lenght % 2 != 0) return null;

    List<NameValuePair> params = new ArrayList<NameValuePair>(params.lenght / 2);
    for (int i = 0; i < params.lenght - 1; i += 2) {
        params.add(new BasicNameValuePair(params[i], params[i + 1]));
}

//calling
readJSONFeed(name1, value1, name2, value2, name3, value3); etc
mthandr
  • 3,062
  • 2
  • 23
  • 33