3

I have recently been setting up mobile apps to work with my meteor server. As a part of this I have to pass the meteor web app data from android. Unfortunately I have been receiving a error that tells me that the java object I am passing "would be serialized to null". How do I prevent this?

     JSONObject json = new JSONObject();
            try{
                json.put("Foo", "1");
                json.put("Blah", 0);
            }catch (JSONException e){

            }
            Object[] object = new Object[1];
            object[0] = json;
            System.out.println(object + ", " + object[0] + ", " + object[0].toString());
            mMeteor.call("xxx", object, new ResultListener() {
                @Override
                public void onSuccess(String result) {

                }
                @Override
                public void onError(String error, String reason, String details) {

                }
            });
        }

        @Override
        public void onError(String error, String reason, String details) {

        }
    });

Android/Meteor interface Library function

 public void callWithSeed(final String methodName, final String randomSeed, final Object[] params, final ResultListener listener) {
    // create a new unique ID for this request
    final String callId = uniqueID();

    // save a reference to the listener to be executed later
    if (listener != null) {
        mListeners.put(callId, listener);
    }

    // send the request
    final Map<String, Object> data = new HashMap<String, Object>();
    data.put(Protocol.Field.MESSAGE, Protocol.Message.METHOD);
    data.put(Protocol.Field.METHOD, methodName);
    data.put(Protocol.Field.ID, callId);
    if (params != null) {
        data.put(Protocol.Field.PARAMS, params);
    }
    if (randomSeed != null) {
        data.put(Protocol.Field.RANDOM_SEED, randomSeed);
    }
    send(data);
}
Joshua Unrau
  • 313
  • 1
  • 2
  • 13

2 Answers2

0

I was having this same issue, my first error was passing a CharSequence instead a String as a parameter (your Object[]), and my other error was passing an Object[] as another parameter (I solved this by sending a String instead, like : String.valueOf(your_object_list)) Dont forget to handle this on your server side, you will receive a String instead of an Object.

Tincho825
  • 829
  • 1
  • 13
  • 15
0

Convert the JSONArray to List & JSONObject to HashMap and then pass those instead of the raw JSONObject or JSONArray.

You can write a recursive function for the conversion in case of nested JSONObject and JSONArray or can use GSON library for the conversion.

For more details about the conversion, this SO post may be helpful.

Touhid
  • 731
  • 1
  • 10
  • 25