android json to listview - android

I working with android and JSON. I came so far, but I can's set the ID in the arraylist.The error will be in "json_data.getInt("ID")"
Can sombody help me
public void klussenlijst()
{
ArrayList<String> results = new ArrayList<String>();
JSONObject json_data;
mysql client = new mysql("http://www.****************/klussen.php?actie=klussen");
try {
client.Execute(RequestMethod.POST);
} catch (Exception e) {
e.printStackTrace();
}
String response = client.getResponse();
try{;
JSONArray jArray = new JSONArray(response);
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
results.add(json_data.getInt("ID"),(String) json_data.get("titel").toString());
}
filllist();
}
catch(JSONException e){
e.printStackTrace();
}
}

Not sure about your mysql() method and the huge lack of error handling, but are you sure that your project has allowed for INTERNET permissions in your AndroidManifest.xml file? Sounds to me like your getInt is not an int.

Related

Get Data From Nested Array JSON Without Name Android

I'm trying to get the value of elevations from JSON in android but in the log nothing is shown. This JSON is a nested array, I'm still new toth parsing JSON in android.
This is what the JSON looks like :
This is my code :
try {
JSONObject responseObject = new JSONObject(result);
JSONArray elev = responseObject.getJSONArray("resourceSets");
JSONArray el = elev.getJSONObject(1).getJSONArray("elevations");
for(int i=0; i<=el.length();i++){
Log.d("EL",el.toString());
}
}
catch (Exception e){
e.printStackTrace();
}
try like below: (problem in your code is you are getting "elevations" directly without getting "resources" array)
try {
JSONObject responseObject = new JSONObject(result);
JSONArray elev = responseObject.getJSONArray("resourceSets");
JSONArray el_resources = elev.getJSONObject(0).getJSONArray("resources");
JSONArray el = el_resources.getJSONObject(0).getJSONArray("elevations");
for(int i=0; i<=el.length();i++){
Log.d("EL", el[i].toString()); // also get index from array then print it
}
}
catch (Exception e){
e.printStackTrace();
}

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

json Parsing trouble in android

I get a problem when I parse this text as JSON. First, I created jsonobject from an URL. I can't show data when debugging, can anyone help me?
JSON:
[
{
"Tarih":"21.12.2015",
"Imsak":"05:51",
"Gunes":"07:22",
"Ogle":"12:18",
"Ikindi":"14:39",
"Aksam":"17:02",
"Yatsi":"18:26",
"Kible":"09:41"
}
]
I tried this code to solve it, but the JSON wasn't correctly parsed:
Java Code:
jsonobject = JSONfunctions.getJSONfromURL("http://namazvakitleri.ahmeti.net/index.php?islem=getSehirList&ulke_id=2");
try {
// Locate the NodeList name
jsonarray = jsonobject.getJSONArray("d");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
WorldPopulation worldpop = new WorldPopulation();
worldpop.setCountry(jsonobject.optString("SehirId"));
worldpop.setPopulation(jsonobject.optString("population"));
world.add(worldpop);
// Populate spinner with country names
worldlist.add(jsonobject.optString("SehirAdi"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
'[' indicates a JsonArray whereas '{' indicates a JsonObject. You have assigned the result to the JsonObject. Instead of that assign it to JsonArray.
Also I don't see any key as "d" in you response.
Try this :
jsonarray = JSONfunctions
.getJSONfromURL("http://namazvakitleri.ahmeti.net/index.php?islem=getSehirList&ulke_id=2");
try {
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
WorldPopulation worldpop = new WorldPopulation();
worldpop.setCountry(jsonobject.optString("SehirId"));
worldpop.setPopulation(jsonobject.optString("population"));
world.add(worldpop);
// Populate spinner with country names
worldlist.add(jsonobject.optString("SehirAdi"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
But again please note your response does not contain this keys 'SehirId', 'population' and 'SehirAdi'
Your response is JsonArray not a JsonObject.
Now you have two ways to solve this problem.
change your getJSONfromURL() to return JsonArray
change response from server to return JsonObject instead of JsonArray
after doing above changes you must need to parse it according to your response.
Note : You have catch block available, so please try to check error in logcat and search on google related to that error.
try this solution :
try {
JSONArray jsonArray = new JSONArray("Your Response");
JSONObject jsonObject = (JSONObject) jsonArray.get(0);
Iterator keyNames = jsonObject.keys();
while (keyNames.hasNext()) {
String keyName = (String) keyNames.next();
String keyValue = jsonObject.getString(keyName);
}
} catch (Exception e) {
e.printStackTrace();
}

Android - getString method error

I've got a slight problem with a getString function in my Android code.
I create a string and I want to use it to retrieve a String which is part of a JSON Array but I get the following error:
The method getString(String) is undefined for the type String
This is the specific code for this section:
private void read_JSON()
{
JSONArray jsa2 = new JSONArray();
for (int i=0; i < jsa2.length(); i++)
{
try
{
JSONObject jso2 = new JSONObject();
jso2 = jsa2.getJSONObject(i);
String one = one.getString("Blur");
//esbrinar com arreglar aixo!!
}catch (JSONException e)
{
e.printStackTrace();
}
}
}
"Blur" is a String which is part of a JSONArray, defined here:
private void create_JSON()
{
JSONObject jso = new JSONObject();
try {
jso.put("Nombre","Miguel");
jso.put("Apellidos", "Garcia");
jso.put("Año_nacimiento", 1990);
JSONArray jsa = new JSONArray();
jsa.put("Blur");
jsa.put("Clur");
jso.put("Nombres_Hijos", jsa);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Could you help me understand what I'm doing wrong?
Thank you very much.
Yours sincerely,
Mauro.
jso2.getString("Blur") might be what you're trying to call. I believe you want to extract a string from the JSONObject you just got from JSONArray. What you actually wrote is to extract string from the string you just defined.

Categories

Resources