Max Usanin
Max Usanin

Reputation: 2499

how to send on the server variables float?

I need {"location":{"lat": 50.4, "lng": 30.5}} send on the server

i do this

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"}}

I need to send the type of float not string

Upvotes: 2

Views: 819

Answers (2)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

if you want to send json Obejct to sever then first create it using JSONObject instead of passing jsonobejct's values using NameValuePair . create current JSONObject as :

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

now send json object to sever.

for sending JSONObject to server see

How to send a JSON object over Request with Android?

Upvotes: 1

harmjanr
harmjanr

Reputation: 937

I guess you want to send JSON to the server? Your code gps.toString() creates a string value of your List, not JSON. You should use a JSON library (GSON for example), to parse your List to valid JSON.

With GSON, you can parse your list to JSON like this:

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

Upvotes: 0

Related Questions