tags:

views:

56

answers:

5

Hi Guys,

I'm dealing with doubles and looking for the "double" equivalent of this...

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("lat",lat));
nameValuePairs.add(new BasicNameValuePair("lng",lng));

edit// I'm making a HTTP request, I think need to map the lat value to a php lat variable and the long to a php variable.

Thanks!

+1  A: 

AbstractMap.SimpleEntry is a JDK class representing a generic tuple. Unfortunately, it's an inner class, making its use very, very verbose (especially once you use the type parameters). Additionally, using it with double values requires those to be wrapped (possibly using autoboxing), which adds more overhead.

You might want to write your own StringDoublePair class - it's not exactly difficult.

Michael Borgwardt
+1  A: 

Why don't you use a Map?

Map<String, Double> nameValuePairs = new HashMap<String, Double>();
nameValuePairs.put("lat", lat);
nameValuePairs.put("lng", lng);
frm
Trying to send a HTTP request, If I do that I get a.."The constructor UrlEncodedFormEntity(Map<String,Double>) is undefined"error on line.. httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Ulkmun
+1  A: 

Like this maybe?

ArrayList<double[]> latLons = new ArrayList<double[]>();
latlons.add(new double[]{60.0, 20.0});
Andreas_D
(I'm a bit lost with the question...)
Andreas_D
Well, you see I'm making a HTTP request, I think need to map the lat value to a php lat variable and the long to a php variable. Sorry if it's obvious how to do this, I'm rather new to these concepts.
Ulkmun
+1  A: 

If you have to use NameValuePair, there are two possible solutions.

You can convert a Map to a List of NameValuePair:

List<NameValuePair> convertParameters(Map<String, Double> parameters) {
    List<NameValuePair> result = new ArrayList<NameValuePair>();

    for (Entry<String, Double> entry : parameters.entrySet()) {
        result.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
    }

    return result;
}

Or you can implement a new NameValuePair to handle Double values:

public class DoubleNameValuePair implements NameValuePair {

    String name;

    double value;

    public DoubleNameValuePair(String name, double value) {
        this.name = name;
        this.value = value;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getValue() {
        return Double.toString(value);
    }

}

In the latter case, you can use a List of NameValuePair that can be passed directly to UrlEncodedFormEntity's constructor:

List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new DoubleNameValuePair("lat", 1.0));
parameters.add(new DoubleNameValuePair("lng", 2.0));
frm
A: 

I assume you are talking about NameValuePair used in Apache HttpClient. The value must be string. You have to convert it to String.

This is what I do,

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("lat",""+lat));
nameValuePairs.add(new BasicNameValuePair("lng",""+lng));
ZZ Coder
problem is I then have to do some computation with the Double's so that wont exactly work :(
Ulkmun