android getIntent().getSerializableExtra() returns null - android

I am trying to send bean object(implents Serializable) with 16 strings data obtained from a parser. I am sending that using putExtra("string",serializablevalue) and I'm receiving that using getIntent().getSerializable("string"). I have used this option for almost 10 functionalities it works fine for me.But particular this functionality alone always returns me null in receiving location.I have cross checked it while sending.it has value. While in the receiving location.
My doubt will bean with 16 fields could be sent with this method. Suggest me a better solution for this problem.

Try:
getIntent().getExtras().getSerializable("string")

My code works fine with ArrayList<String>. Could you please give me/us your serializable-strings?
On the other side, based on this: http://developer.android.com/reference/android/os/Parcelable.html, then if you use custom class, you need to "transfer" the data between: public void writeToParcel(Parcel out, int flags) and private MyParcelable(Parcel in).
In details, you can write your data to out, and get them from in.

Check if the String are equals to in both activities!!
it happened to me I had one different capital letter, when I corrected it it worked :)

Related

Android Retrofit's #Path don't work. Why?

I have an app which takes trivia question from api and display. When I use the actual link for take questions it works (https://trivia.willfry.co.uk/api/questions?categories=movies&limit=16 ) . However when I add path for categories, app does not work.
This is work :
#GET("questions?categories=general_knowledge&limit=16")
Call<List<Questions>> general();
However this is not work :
#GET("questions?categories={type}&limit=16")
Call<List<Questions>> getData(#Path("type") String type);
Also when I delete the app from emulator, and install again with path include method, it works just once. Not second time.
categories is not a path parameter, it's a query, do this instead
#GET("questions?limit=16")
Call<List<Questions>> getData(#Query("categories") String categories);
and about the second problem you mentioned, there's noting in the provided code to find out from

Firebase Database Error - Expected a Map while deserializing, but got a class java.util.ArrayList

Edit: Figured it out, check my posted answer if you're having similar issues.
I know there are several questions about this issue, but none of their solutions are working for me.
In my model class I have made sure to use List instead of Arraylist to avoid Firebase issues, but am still getting this error. It's a lot of code but most questions ask for all the code so I'll post it all.
TemplateModelClass.java
//
I've used this basic model successfully many times. For the
HashMaps<String, List<String>>,
the String is an incremented Integer converted to String. The List's are just Strings in a List. Here's some sample JSON from Firebase:
//
Formatted that as best as I could. If you need a picture of it let me know and I'll get a screenshot
And am getting this error, as stated in the title:
com.google.firebase.database.DatabaseException: Expected a Map while deserializing, but got a class java.util.ArrayList
The most upvoted question about this seems to have something to do with a problem using an integer as a key, but I think I've avoided that by always using an integer converted to a string. It may be interpreting it strangely, so I'll try some more stuff in the meantime. Thanks for reading!
Alright, figured it out. If anyone reading this has this problem and are using incremented ints/longs/whatever that get converted to strings, you must add some characters to the converted int. Firebase apparently converts these keys back into non-Strings if it can be converted.
For example, if you do something like this:
int inc = 0;
inc++; // 1
map.put(String.valueOf(inc), someList);
Firebase interprets that key as 1 instead of "1".
So, to force Fb to intepret as a string, do something like this:
int inc = 0;
inc++; // 1
map.put(String.valueOf(inc) + "_key", someList);
And everything works out perfectly. Obviously if you also need to read those Strings back to ints, just split the string with "[_]" and you're good to go.
The main issue is that you are using a List instead of a Map. As your error said, while deserializing it is expectig a Map but is found an ArrayList.
So in order to solve this problem youd need to change all the lists in your model with maps like this:
private Map<String, Object> mMapOne;
After changing all those fileds like this, you need also to change your public setters and getters.
Hope it helps.

Injecting entity object while calling google cloud endpoint from android

How do I pass the arraylist to google cloud endpoint? It doesn't seem to work.
Edit start ----
Here is the signature of the endpoint with arraylist as input
public CollectionResponse<String> listDevices(#Named("devices") ArrayList<String> devices)
However when I iterate over this arraylist, I get all the records condensed into one. So even though I pass 10 strings, I get only one in my endpoint.
Edit end ----
I read somewhere that I should create a wrapper entity for arraylist and then pass it.
Edit start ----
So I created the entity containing the arraylist
#Entity
public class DEviceList {
ArrayList<String> devices;
}
and modified the signature as -
public CollectionResponse<String> listDevices(DeviceList devices)
Is it possible to pass object of DeviceList from client even though it not #Named? Can you provide the syntax?
My understanding is that since it's an entity it cannot be #Named, so while calling I need to inject it.
But google allows only three injected types -
1. com.google.appengine.api.users.User
2. javax.servlet.http.HttpServletRequest
3. javax.servlet.ServletContext
So above signature would not work.
So I changed the signature to -
public CollectionResponse<String> listDevices(HttpServletRequest request)
and inside I could get the entity as
DeviceList deviceList = (DeviceList)request.getAttribute("deviceList");
However I am not sure how to call this endpoint from the android client?
How I do pass the entity object using HTTPServletRequest?
Edit end ----
How do I do that? Can anyone cite an example?
The way you word your question, it seems the call is silently failing. That can't be. You must be receiving some kind of exception or log somewhere which could help you identify your issue better. You could read this article to refresh on cloud endpoint APIs and android.
If you are having trouble passing an arraylist of objects from your client to the API, I would suggest checking some things:
does the argument type in the API match what is being sent from the client? Do they both have access to the class definition?
if the data type inside the arraylist is a primitive, and it still fails, perhaps the advice you read "somewhere" was referring to the need (although I don't think this is the case) to use a wrapper object which simply contains one field - the arraylist, and pass that along the line?
If either the reminder to check your logs/error messages or any other info in this answer helped you resolve the issue, please remember to accept it, but not without first editing your post to explain how you resolved your issue.

Android: Parcelable class that contains a parcelable field

NOTE: the solution is in the answer starting with SOLUTION that I can't accept since it's my own anwer. I hope this helps someone else.
I want to make my class, that includes a parcelable object, parcelable. In other words let's say I have a class with 3 fields: String name, int id and Location loc. Now I want to make this class Parcelable. How can I do it? So far that's what I did:
public MyClass(Parcel in) {
name = in.readString();
id = in.readInt();
loc = in.readParcelable(Location.class.getClassLoader()); }
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(id);
dest.writeParcelable(loc, flags); }
When I go to use it I get and error: Class not found when unmarshalling...
Does anyone know why I get this error and how to fix it?
Thanks in advance for any answer.
EDIT:
I have to add that since I'm sending this data to a service working on his own process I'm using the Handler technique so maybe that could be the problem. But still that method is done well since it was working perfectly until I introduce the Location object in MyClass.
SOLUTION: I found the solution. My implementation was fine. The problem was that I send the message at a separate process so I need to set the proper classLoader in order to make it work right. So the upper code is correct and the thing I had to change was when I go to retrive the object from the bundle or the message. I'll paste the link that gave me the solution even if nobody set it as the correct answer: Problem unmarshalling parcelables
This part of the code seems right.
Make sure that inside Location you do the Parcel stuff correctly as well.
One other thing that could go wrong is, that if you serialized the class before you had the Location in it and now you are trying to de-serialize the old object which doesn't have Location yet.

How to present parsed JSON in readable format to users?

So I have JSON, I parsed in and it's sitting there. From my code below it looks like I have put everything into an ArrayList but then what? I mean for example I need the "title" of each JSON object to be an onClick on the first page, is that possible?
Essentially my onPostExceute() is empty/not doing much. Eventually I need to separate each object into it's own page via the onlicks I'm mentioning, but I think I can do that by separating the JSONObjects...? guess I'll come to that when I can.
If I want to separate things should I even be using an ArrayList? It's just what I used for a server test I ran with different code.
Would really appreciate some help. Basically stuck at the last hurdle is how I perceive it. Maybe I'm wrong though. The logs see that the JSON is showing up as one big chunk.
Edit: Removed my code, this is more of a theory question. ListView being the best thing to go with.
yes you can make spearate arraylist for them..and can store them in diferent listviews...on google click you can open new listview showing id and link for google ..and same you can do for microsoft and your other trem.And using onItemClick is a gud option,you can easily get the index of item clicked//

Categories

Resources