Monday, March 31, 2014

Create Json string Format to POST Json data to the Service-- With Array

 http://www.javacodegeeks.com/2013/10/android-json-tutorial-create-and-parse-json-data.html


public class JsonUtil {

public static String toJSon(Person person) {
      try {
        // Here we convert Java Object to JSON 
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("name", person.getName()); // Set the first name/pair 
        jsonObj.put("surname", person.getSurname());

        JSONObject jsonAdd = new JSONObject(); // we need another object to store the address
        jsonAdd.put("address", person.getAddress().getAddress());
        jsonAdd.put("city", person.getAddress().getCity());
        jsonAdd.put("state", person.getAddress().getState());

        // We add the object to the main object
        jsonObj.put("address", jsonAdd);

        // and finally we add the phone number
        // In this case we need a json array to hold the java list
        JSONArray jsonArr = new JSONArray();

        for (PhoneNumber pn : person.getPhoneList() ) {
            JSONObject pnObj = new JSONObject();
            pnObj.put("num", pn.getNumber());
            pnObj.put("type", pn.getType());
            jsonArr.put(pnObj);
        }

        jsonObj.put("phoneNumber", jsonArr);

        return jsonObj.toString();

    }
    catch(JSONException ex) {
        ex.printStackTrace();
    }

    return null;

}
 
 
{
   "phoneNumber": [
      {
         "type": "work",
         "num": "11111"
      },
      {
         "type": "home",
         "num": "2222"
      }
   ],
   "address": {
      "state": "World",
      "address": "infinite space, 000",
      "city": "Android city"
   },
   "surname": "Swa",
   "name": "Android"
}
========================================================
 
 

Create Json string Format to POST Json data to the Service


Task:Register
{
    "Device":
    {
        "DeviceId":"test-iSWzc6l8hEtvxSaaYEPVWlajSgClaPG/ug1xV5dftfQ=",
        "Operator":"Operator",
        "DeviceName":"XDeviceEmulator",
        "DeviceTotalMemory":1023,
        "ScreenWidth":480.0,
        "ScreenHeight":800.0,
        "OperatingSystem":"Android"
    },
    "Receiver":
    {
        "CountryCode":91,"Mobile":"9725910991"
    }
}
JSONObject json1 = new JSONObject();
JSONObject jsonDevice = new JSONObject();
    jsonObj.put("DeviceId", value);
    jsonObj.put("Operator", value);
    jsonObj.put("DeviceName", value);
    jsonObj.put("DeviceTotalMemory", value);
    jsonObj.put("ScreenWidth", value);
    jsonObj.put("ScreenHeight", value);
    jsonObj.put("OperatingSystem", value);
json1.put("Device", jsonDevice);
JSONObject jsonReceiver = new JSONObject();
    jsonObj.put("CountryCode", value);
    jsonObj.put("Mobile", value);
json1.put("Receiver", jsonReceiver);

Strint s1="Task:Register"+json1.toString()+"<EOF>";
Toast.makeText(getBaseContext(), "Json Strint => "+ s1 ,Toast.LENGTH_SHORT).show();

Tuesday, March 25, 2014

Android | Send “POST” JSON Data to Server