Parse Query does not retrive me updated data from the cloud - android

I have an ParseObject in the cloud database. At the begining this object had an JSONArray that contained 2 JSONObjects. Then I run the next code:
Toast.makeText(getActivity(), "List lenght: " + new ParseQuery<MyParseObject>("MyParseObject").get("objectId").getJSONArray("MyJSONArray").length(), Toast.LENGTH_SHORT).show();
This show the result: "List lenght: 2". Well, the problem is when I manually delete a JSONObject from that List in the cloud, and later run the same code, the result is the same, but it should be: "List lenght: 1". I can see perfectly that there are only one JSONObject in the JSONArray.
What could happen?

Check once more your table, better on parse account site.
Seams to me, your field looks like [,object]. Thats why you have length of array = 2 but one of your object in array is empty, and you have only one valid JSON object.

Related

update specific item at Android GridAdapter

I have an project where devices with Tasmota firmware are sending out MQTT JSON formated messages, that are used in my project to build JSONArray using .put method. Later the JSONArray changes are propagated to GridView adapter calling notifyDataSetChanged().
The challenge is to keep JSONArray structure same but with upto date (new MQTT messages arrives) data which arrive in different order then the initial JSONArray structure was. The result is that JSONArray embedded JSONObjects order changes on the fly and the gridView order changes too, that is confusing users. Temporally I tried to solve that by deleting latest JSON message from device that is sending the update from JSONArray by calling .remove and the re-adding updated Json Object calling .put method + calling notifyDataSetChanged() to reflect changes in the GridView.
//on new message arrived
sonoff_obj = mqtt.message;
for(json_position_index = 0; json_position_index < _arr_mqtt.length(); json_position_index++) {
if (_arr_mqtt.getJSONObject( json_position_index ).getString( "Topic" ).equals( _received_topic )){
_arr_mqtt.remove( json_position_index );
_arr_mqtt.put( sonoff_obj );
break;
}
}
Problem is now, that because using .put, then the new JsonObject is always added to last position in the JSONArray, also GridView layout gets reorganised, which confuses end users.
I am looking for solution how to update JsonObject in the JSONArray maintaining same index(position in the Array) or how to directly say the adapter to replace Item at specific position.
So far i can get adapter item content using .getItem( int index ); but can I tell the adapter something like in (my pseudo code) .setItem( int index , JsonObject obj) ?
For those who are facing the same challenge, ArrayList is much better choice than JsonArray, because it supports .set method which is with .notifyItemChanged() method powerful approach to modify specific position in the ArrayList. If you need for some reason to keep JSONArray format (for GridAdapter ie.), you can always export ArrayList into JSONArray like this (and of course call notifyDataSetChanged()):
my_mqtt_array = new JSONArray( my_arrayList.toString());
my_mqtt_array.notifyDataSetChanged();

could not obtain extracted text error on iterate an array

I'm beginner in Android and maybe my question be so simple.
I have defined a simple array and then push some strings from DB. like this :
db=new mydatabasehandlerTile(this);
mylist = new ArrayList<String>();
mylist=db.getfavoriteslist();
for(int i=1;i<=mylist.size();i++) {
Toast.makeText(this,String.valueOf(mylist.get(i)), Toast.LENGTH_SHORT).show();
}
As you can see I want to show each of array string item in Toast. but whenever I run my app I got this error and app is crashed :
onStartInput event aborted: com.touchtype.keyboard.h.p: could not obtain extracted text (class com.touchtype.keyboard.h.p)
I do not know what is problem. can anyone help me?
The obvious error that I see in your code is that you don't take in account that the index of the list is zero based, so change to this:
for(int i=0;i<mylist.size();i++) {
Toast.makeText(this,String.valueOf(mylist.get(i)), Toast.LENGTH_SHORT).show();
}

Python json/list to dictionary cast not working

I'm currently deveolping an Android application that has Django framework as it's server side.
When i'm posting a data of a new user to my database i am POSTing a multipart request that has a user part inside.
The user for some reason is represented as a list but when i take it out of the request.data['user'] it's a str instance (Yea i dont know why...)
When i fetch that str i started working on it with json package.
I looked up on the internet (to many places..) how to convert a string in json format to a dictionary.
What i found is that when you use the json.loads command it doesn't give a dict back but a str instance :)
This is the code on my server side when i enter the create function of the ModelViewSet that handles the creation of the user.
userJson = request.data['user']
userJson = json.dumps(userJson)
userJson = json.loads(userJson)
What i tried to do is to make a string of my own in JSON format and that called the json.loads() command which gave me the dict object..
There seems to be a problem with processing the str from the http request of django rest framework for some reason or there's something else i am not seeing.
I tried the following links -
Converting JSON String to Dictionary Not List
http://docs.python-guide.org/en/latest/scenarios/json/
Didn't worked also..
Now, i tried accessing the str i got from json.loads() like a dictionary in this way.
id = userJson['id']
Now lets say maybe i passed a wrong json format to the loads function, it should have thrown an exception..
The code above (getting the id) raised an exception of 'String indices must be integer' - it doesn't convert it to dict! LOL xD
Good note worth mentioning - I'm trying to convert the json to a dictionary so i could access it like this - dictObject['id']
Well i would really appreciate every help!
Thanks :)
For some reason , when i did this commands-
userJson = request.data['user']
userJson = json.loads(userJson)
userJson = json.loads(userJson)
What i got to have inside the userJson after the second json.loads(userJson) I got the actual dict object to the userJson member.
Appearently it is a bug.
21 January - another update, I truly was doing double Json encoding on the Android application so that was the reason for double json. loads()

Android JSON fetching - No value for (value) eventhough the value exists

I have a valid json link, and I want to fetch data from it.
This is my code, for fetching data:
try {
JSONObject topicsObject = new JSONObject(getTopicsJSON);
JSONArray topics = topicsObject.getJSONArray("Topics");
for (int i = 0; i<topics.length();i++){
SuperTrenerTopic stt = new SuperTrenerTopic();
JSONObject sub_topic = topics.getJSONObject(i);
JSONObject topic = sub_topic.getJSONObject("Topic");
...
}
On the last line of code, I get ann error saying org.json.JSONException: No value for Topic.
Following screenshot shows that it is not correct, as I Log-ed the recieved JSON, and you can clearly see that in fact, there IS a "Topic" object out there:
First Log represents the first part of an entire Json responce, and the second one is the Topic object itself.
Here is another screenshot of the JSON format i took from my browser:
You can notice that "Topic" and "meta" objects are on the same hierarchy level.
I fetch "meta" object with no problem whatsoever, using the same code I would use for fetching 'Topics", but for some reason, fetching "Topics" doesn't work out.
here is the code I successfully use to fetch "meta' object:
JSONObject meta = sub_topic.getJSONObject("meta");
What could be the cause of this?
Is there something realy obvious that I'm missing out?

Iterating through JsonObject in Java/Android. NOT JsonArray

Here's my JSON data sample:
{
"node_1.1":{
"someCrap":{
"someCrap":"SomeValue"
}
},
"node_1.2":{
"Node_1.2.1":{
"Node_1.2.1.1":{
"param1":value,
"param2":value,
"param3":"value",
"paramThatIneed":{
"ThisIsWhatIActuallyNeed":"url",
"width":96,
"height":72
}
},
"Node_1.2.1.2":{
Same as above, just that paramThatINeed might be missing, gotta place imagePlaceHolder Here
},
//and so on... there are a total of 50 of these..
}
}
}
Now I could get the node_1.1 and Node 1.2 and the sub-node of it node_1.2.1
However, there are 50 sub-nodes inside of node_1.2.1, and they will have random names returned from the server. Its in string format but they're actually ints. Its a page ID.
Now I wanna iterate through the node_1.2.1 and get those sub-nodes and access their sub-nodes and take in the URL of the paramThatINeed. If the paramThatINeed is not present, I need to put some null/dummy value.
This is the code that I tried to work it as far as I've reached:
JSONObject jsonObj = new JSONObject(jsonStr); //jsonStr is the entire JSON string
JsonObject node_1.2= jsonObj.getJsonObject("node_1.1");
JsonObject node_1.2.1 = node_1.2.getJsonObject("node_1.2.1");
What do I do after this? Because I can only getJsonObject by passing a string param to it, I tried using the for loop but it doesn't take any int param.
Also, as I said before, the nodes after that have random names and not fixed. So I'm totally confused.
Please help me out if you know how to solve this problem. Please remember there's no JsonArray in this. I'm probably thinking of editing the JSON string itself and replacing some parts of the '{' with '[' and converting it to an array :( ... I think that's a sad approach.
Use this to iterate over an object.
Android (JSONObject) How can I loop through a flat JSON object to get each key and each value
but be careful, from json object you won't get the result in original order, like in json array. The result will be in alphabetical order (I hope I was clear). And you can use optJsonobject(), instead of getJsonObject(). It will returns null, instead of throw exception. You can use opt every where instead of get.

Categories

Resources