开发者

Numerical equivalent of nameValuePair array list?

I'm dealing with doubles and looking for the "d开发者_如何学JAVAouble" 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!


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));


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.


Why don't you use a Map?

Map<String, Double> nameValuePairs = new HashMap<String, Double>();
nameValuePairs.put("lat", lat);
nameValuePairs.put("lng", lng);


Like this maybe?

ArrayList<double[]> latLons = new ArrayList<double[]>();
latlons.add(new double[]{60.0, 20.0});


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));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜