Sending json object to server in android - android

I am trying to send json to HTTP server with this JSON format:
{"deviceID":3852883413434, "depth":2,"location":{"xCord":46.232, "yCord":42.4421},"size":3}
and here is my code in android, but seems not working well.Maybe i need array for location or format my code.Any adivce could help thanks.
JSONObject postData = new JSONObject();
try {
postData.put("deviceID", unique_id);
postData.put("depth",((HoleSize)this.getApplication()).getDepth());
postData.put("xCord",((HoleSize)this.getApplication()).getLattitude());
postData.put("yCord",((HoleSize)this.getApplication()).getLongitude());
postData.put("size", ((HoleSize) this.getApplication()).getSize());
new SendData().execute("servername",postData.toString());
} catch (JSONException e) {
e.printStackTrace();
}

Using the json response,
{"deviceID":3852883413434, "depth":2,"location":{"xCord":46.232,
"yCord":42.4421},"size":3}
you must get the JSON Object to get the value of depth and size (contained in the same object) and then get the JSON Object of location to get the values of xCord, yCord:
try {
JSONObject postData = new JSONObject(response);
String depth = postData.get("depth").toString();
String size = postData.getInt("size").toString();
JSONObject locationObject = new JSONObject(postData.get("location").toString());
String xCord = locationObject.get("xCord").toString();
String yCord = locationObject.get("yCord").toString();
} catch (JSONException e) {
e.printStackTrace();
}
applying this to your code:
try {
JSONObject postData = new JSONObject(response);
postData.put("deviceID", unique_id);
postData.put("depth",postData.get("depth").toString());
//postData.put("xCord", locationObject.get("xCord").toString());
postData.put("location", postData.get("location").toString());
postData.put("yCord",locationObject.get("yCord").toString());
postData.put("size", postData.getInt("size").toString());
new SendData().execute("servername",postData.toString());
} catch (JSONException e) {
e.printStackTrace();
}

Related

Android - How to parse intent to json object?

I want to add my intent to json object, but how to do that with this code below
Pojo.java
//getter and setter on above code
JSONObject obj = new JSONObject();
try {
obj.put("id", id);
obj.put("nilai", ratingStar);
} catch (JSONException e) {
e.printStackTrace();
}
return obj;
}
Main.java
for (int i=0;i<size;i++){
jArray.put(adapter3.getItem(i).getJsonObject());
}
JSONObject j=new JSONObject();
try {
j.put("fishing",jArray);
Log.d("json output : ",j.toString());
} catch (JSONException e) {
e.printStackTrace();
}
And the respone from Log.d("json output : ",j.toString()); is :
{"fishing":[{"id":"1","nilai":1},{"id":"2","nilai":1},{"id":"3","nilai":1},{"id":"4","nilai":1},{"id":"5","nilai":1}]}
But i want to add my intent into json object like this
{"fishing":[{|"id_dosen":"2","id_matkul":"3"|,"id":"1","nilai":1},{|"id_dosen":"2","id_matkul":"3"|,"id":"2","nilai":1},{|"id_dosen":"2","id_matkul":"3"|,"id":"3","nilai":1},{|"id_dosen":"2","id_matkul":"3"|,"id":"4","nilai":1},{|"id_dosen":"2","id_matkul":"3"|,"id":"5","nilai":1}]}
can someone help me how to add the json that I mark with |??
Note : i use | symbol just to let you guys easy to read what's the different from 1st json with 2nd json
You can simply add those fields in your JSON like this.
String jsonFishing = "{\"fishing\":[{\"id\":\"1\",\"nilai\":1},{\"id\":\"2\",\"nilai\":1},{\"id\":\"3\",\"nilai\":1},{\"id\":\"4\",\"nilai\":1},{\"id\":\"5\",\"nilai\":1}]}";
try {
JSONObject rootObj = new JSONObject(jsonFishing);
JSONArray fishingArr = rootObj.getJSONArray("fishing");
for (int i = 0; i < fishingArr.length(); i++){
JSONObject child = fishingArr.getJSONObject(i);
child.put("id_dosen", 2);
child.put("id_matkul", 3);
}
...
yourIntent.putExtra("json", rootObj.toString());
} catch (JSONException e) {
e.printStackTrace();
}
Newly obtained JSON looks like this.
{"fishing":[{"id":"1","nilai":1,"id_dosen":2,"id_matkul":3},{"id":"2","nilai":1,"id_dosen":2,"id_matkul":3},{"id":"3","nilai":1,"id_dosen":2,"id_matkul":3},{"id":"4","nilai":1,"id_dosen":2,"id_matkul":3},{"id":"5","nilai":1,"id_dosen":2,"id_matkul":3}]}
Edit : In your code, it would be like this
for (int i=0;i<size;i++){
JSONObject jsonObject = adapter3.getItem(i).getJsonObject();
jsonObject.put("id_dosen", 2);
jsonObject.put("id_matkul", 3);
jArray.put(jsonObject);
}
Hope this helps :)

JSONArray and ArrayList getting replaced for every value insertion

JSONArray getting replaced for every value insertion
In this below output I am getting replaced by old jArrayFacebookData.
or I need those json data should be add to string
dbchooseuserid,dbchoosebookid,dbchoosechapterid,dbchoosequestionid, Type,dbchooseoptionidthose valus are from db with those values I need to make json string
I need output like this
string h =[{"user_id":"1","book_id":"64","chapter_id":"1","question_type":"hint","question_id":"1","notes":",,Gg,,,,"},{"user_id":"1","book_id":"64","chapter_id":"1","question_type":"choose","question_id":"1","option_id":"1"}]
This is my code
JSONArray jArrayFacebookData = new JSONArray();
JSONObject jObjectData = new JSONObject();
ArrayList<String> a= new ArrayList<String>();
if(TypeHint.equals("hint"))
{
try {
jObjectData.put("user_id", dbhintuserid);
jObjectData.put("book_id", dbhintbookid);
jObjectData.put("chapter_id", dbhintchapterid);
jObjectData.put("question_type", TypeHint);
jObjectData.put("question_id", dbhintquestionid);
jObjectData.put("notes", dbhintnotes);
jArrayFacebookData.put(jObjectData);
Log.i("tag555", ""+jArrayFacebookData.toString());
a.add(jObjectData.toString());
Log.i("tag5", ""+a);
} catch (JSONException e) {
e.printStackTrace();
}}
if(Type.equals("choose"))
{
try {
jObjectData.put("user_id", dbchooseuserid);
jObjectData.put("book_id", dbchoosebookid);
jObjectData.put("chapter_id", dbchoosechapterid);
jObjectData.put("question_type", Type);
jObjectData.put("question_id", dbchoosequestionid);
jObjectData.put("option_id", dbchooseoptionid);
jArrayFacebookData.put(jObjectData);
Log.i("tag555", ""+jArrayFacebookData.toString());
a.add(jObjectData.toString());
Log.i("tag5", ""+a);
} catch (JSONException e) {
e.printStackTrace();
}}
Output I am getting is
[{"user_id":"1","book_id":"64","chapter_id":"1","question_type":"choose","question_id":"1","option_id":"1"}]
But I need output like this
[{"user_id":"1","book_id":"64","chapter_id":"1","question_type":"hint","question_id":"1","notes":",,Gg,,,,"}{"user_id":"1","book_id":"64","chapter_id":"1","question_type":"choose","question_id":"1","option_id":"1"}]
Instantiate JSONObject after every if condition and then try.
if(TypeHint.equals("hint")){
JSONObject jObjectData = new JSONObject();
......
if(Type.equals("choose")){
JSONObject jObjectData = new JSONObject();
....

How to create a JSON

I was create a JSON Object like below but i don't know how can fill parameters field with JSONStringer? And parameters field is a JSONArray or Array String?
{"name":"Katy", "parameters":["JAK","1999"], "Age":25}
Thanks for your help .
Try like below
String mParameters[] = { "JAK", "1999" };
JSONObject mJson = new JSONObject();
try {
mJson.put("name", "Katy");
JSONArray mJSONArray = new JSONArray(Arrays.asList(mParameters));
mJson.putOpt("parameters", mJSONArray);
mJson.put("Age", 25);
System.out.println("JSon::"+ mJson.toString());
} catch (JSONException e) {
e.printStackTrace();
}
The documentation for JSON will show you that an easy way to distinguish wheter your JSON string is an array is whether it starts with a [, so "parameters" in your example is a JSONArray.
Without knowing how you will be getting the data you want to put in your object, here is an example of how you would populate it (assuming you have an array of JAKS to insert).
JSONObject yourObject = new JSONObject();
String[] JAKS = {"1999", "2000", "2001"};
JSONArray paramaters = new JSONArray();
try {
yourObject.put("name", "Katy");
for (String JAK : JAKS) {
JSONObject yourParamater = new JSONObject();
yourParamater.put("JAK", JAK);
paramaters.put(yourParamater);
}
yourObject.put("parameters", paramaters);
yourObject.put("Age", 25);
} catch (JSONException e) {
e.printStackTrace();
}
Try this:
import org.json.JSONObject;
//other imports
//...
try {
//Create the JSON Object
JSONObject myObject = new JSONObject();
String parameters[] = new String[]{"JAK","1999"};
//use the method put to "fill" the values
myObject.put("name", "Katy");
myObject.put("parameter",(Object)parameters);
myObject.put("age", 25);
} catch (JSONException e) {
e.printStackTrace();
}
Try this way,hope this will help you to solve your problem.
try{
JSONObject jsonObject = new JSONObject();
jsonObject.put("name","Katy");
jsonObject.put("parameters",new String[]{"JAK","1999"});
jsonObject.put("Age","25");
}catch (Throwable e){
e.printStackTrace();
}

Create Jsonstring - Android

Hi i want to create json string like
{"id":"12345","option-ids":["100"]}
i've tried like below
JSONObject object = new JSONObject();
try {
object.put("id","12314");
JsonArray jsonArray = new JsonArray();
jsonArray......
object.put("option-ids",""+jsonArray);
} catch (JSONException e) {
e.printStackTrace();
}
but i struck on create json array withou object name.
// First - create the json Object
JSONObject object = new JSONObject();
try {
// Add the id to the json
object.put("id", "12314");
// Create a json array
JSONArray jsonArray = new JSONArray();
// Put the values you want
jsonArray.put("1");
jsonArray.put("2");
object.put("option-ids",jsonArray.toString());
} catch (JSONException e) {
// Handle impossible error
e.printStackTrace();
}
To insert String in JSONArray,use put() method of JSONArray.
try below code.
JSONObject object = new JSONObject();
try {
object.put("id","12345");
JSONArray jsonArray = new JSONArray();
jsonArray.put("100"); //add here
object.put("option-ids",jsonArray.toString());
} catch (JSONException e) {
e.printStackTrace();
}
You should use JSONArray (from org.json) instead of JsonArray(from com.google.gson)

Android parsing json file

Json string contained in String str:
{conf:{"quantity_uom":"l",price_uom:"euro",distance_uom:"km",consumption_uom:"km/l"}}
Code:
try{
if (str!=""){
this.json = new JSONObject(str);
this.quantityUom=this.json.getString("quantity_uom");
this.distanceUom=this.json.getString("distance_uom");
this.priceUom=this.json.getString("price_uom");
this.consumptionUom=this.json.getString("consumption_uom");
}
}catch (Exception e) {
throw e;
}
I have returned No value for quantity_uom. How I am doing wrong? String contain the json text.
Thank you so much.
try{
if (!str.equals("")){
Json obj = new JSONObject(str);
this.json = obj.getJSONObject("conf");
this.quantityUom=this.json.getString("quantity_uom");
this.distanceUom=this.json.getString("distance_uom");
this.priceUom=this.json.getString("price_uom");
this.consumptionUom=this.json.getString("consumption_uom");
}
}catch (Exception e) {
throw e;
}
You first have to get json object named 'conf' from string and from that json object get other values.
The JSON string is not correct.
Try this:
{"conf":{"quantity_uom":"l","price_uom":"euro","distance_uom":"km","consumption_uom":"km/l"}}

Categories

Resources