2013-02-19 51 views
2

我需要{"location":{"lat": 50.4, "lng": 30.5}}发送服务器上如何在服务器上发送变量float?

我这样做

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
List<NameValuePair> gps = new ArrayList<NameValuePair>(2); 
      gps.add(new BasicNameValuePair("lat", Util 
        .getLatitude())); 
      gps.add(new BasicNameValuePair("lng", Util 
        .getLatitude())); 

      nameValuePairs.add(new BasicNameValuePair("location",gps.toString())); 

{"location":{"lat": "50.4", "lng": "30.5"}}

我需要发送

+0

为什么你不只是将其丢在服务器端从字符串到浮动? – 2013-02-19 08:47:15

+0

因为客户想要,我必须了解我可以发送浮动吗? – 2013-02-19 08:49:57

回答

1

随着GSON,您可以分析您的列表,以JSON这样。创建当前的JSONObject为:

// main jsonObject 
JSONObject json = new JSONObject(); 
// location JSONObject 
JSONObject jsonlocation = new JSONObject(); 

// put key-value in jsonlocation JSONObject 
jsonlocation.put("lat", Util.getLatitude()); //<< put lat 
jsonlocation.put("lng", Util.getLatitude()); //<< put lng 

// put jsonlocation in main json JSONObject 

json.put("location",jsonlocation); 

现在发送json对象断绝。

发送的JSONObject服务器看到

How to send a JSON object over Request with Android?

0

我猜你想送浮动不是字符串类型JSON到服务器?您的代码gps.toString()将创建List的字符串值,而不是JSON。您应该使用JSON库(例如GSON)来将您的List解析为有效的JSON。如果你想发送JSON Obejct断绝然后使用JSONObject,而不是使用NameValuePair传递jsonobejct的价值观首先创建它

Gson gson = new Gson(); 
String json = gson.toJson(list);