0

I am trying to send a reponse object (soapobject) to another activity.

Note: I am not creating the object , it is recieved as a response from a webservice.

My logic in the first activity is that if the response contains one or more results ,then send it to the next activity for further processing (otherwise show a "no results" message)

MY problem : putExtra does not support sending an non serilaized or non parcelable object.

Error message : The method putExtra(String, boolean) in the type Intent is not applicable for the arguments (String, SoapObject)

here is my code any suggestions ?

SoapObject soapResponse =  soaphttp.fetchNextCatalogueRange(0, numberOfItems);
Intent ResultsActivityIntent = new   Intent(MainActivity.this,SearchResultsActivity.class);
// Send data object with the Intent
ResultsActivityIntent.putExtra("ResultObj",  soapResponse);
startActivity(ResultsActivityIntent);
Ahmed Ekri
  • 4,601
  • 3
  • 23
  • 42
  • http://stackoverflow.com/questions/2736389/how-to-pass-object-from-one-activity-to-another-in-android – Pararth Feb 13 '14 at 11:47
  • 1
    You should implement `Parcelable`. Check out this answer[How to pass object between activities](http://stackoverflow.com/questions/2736389/how-to-pass-object-from-one-activity-to-another-in-android) – Apoorv Feb 13 '14 at 11:48
  • Cant use parcelable as I'm not creating the object , I'm recieveing it as a search result its not cast-able. – user1882582 Feb 13 '14 at 11:56

2 Answers2

1

Create Constant Class

Create SoapObject in that class like.

 Class Constant
 {
   public static SoapObject sopObj=null;
 }

then here,

 SoapObject soapResponse =  soaphttp.fetchNextCatalogueRange(0, numberOfItems);
   Intent ResultsActivityIntent = new   Intent(MainActivity.this,SearchResultsActivity.class);

// Send data object with the Intent

  Constant.sopObj=soapResponse;

      startActivity(ResultsActivityIntent);

In your Next class

get that soapObject value like this,

  SoapObject sopObject=Constant.sopObj;

soo simple...

Karthick pop
  • 616
  • 3
  • 16
  • Thank you. This is a good and Alternative solution using a constant which all activities can see.... but I would still like to know how to use the intent properly because I think its better practice. – user1882582 Feb 13 '14 at 12:28
0

There is also another option... use Gson and convert your object into a json-String, pass it over Bundle-String to your activity and use Gson again to revert it back from json-String to your object.

Luser_k
  • 514
  • 1
  • 4
  • 18
  • Thanks. I'm not familiar on how to do this , do have any pointers ? – user1882582 Feb 13 '14 at 12:43
  • Check this out, its really good documented... https://sites.google.com/site/gson/gson-user-guide – Luser_k Feb 13 '14 at 14:12
  • No success yet ... I tried gson.toJson(soapResponse) and got an error--> java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: org.ksoap2.serialization.SoapPrimitive. Forgot to register a type adapter? – user1882582 Feb 13 '14 at 14:58
  • I've done some more investigation and it looks like we are going down the 'serialization' path with this suggestion. As I'm not creating the object (and hence cannot predict its contents) it cannot be serialized (hoping... I'm wrong about that). Another solution may be to extract the objects data into a serializable object then use that object in putExtra – user1882582 Feb 14 '14 at 11:53
  • @user1882582 seems like soapResponse has references, that Gson cant handle it! So I would recommend Karthick ´s solution. – Luser_k Feb 14 '14 at 21:27