0

I have a special requirement that I want to pass a HttpResponse(from Apache CORE API) object as an extra to another Intent.

Problem: I have my HTTP server(Apache CORE) running a service S. While I receive a POST request, the response should be sent after execution of an intent A, which sends a Broadcast to my service S already running (which is designed based on my requirement). So I need to handle the HTTP POST response in the Broadcast receiver. Is this possible to do? So, I need the HttpResponse in the handle() method to be sent in the Broadcast receiver and send the response from there. Please let me know if you need more details.

I found links for passing primitive types like String, Integer types etc. Any help is appreciated.

Mark Nottingham
  • 5,546
  • 1
  • 25
  • 21
user1741274
  • 759
  • 3
  • 13
  • 25
  • You can really only pass objects which implement `Parcelable` through intents. Consider a static singleton or parsing the response in your background thread and passing the parsed results. – 323go Jan 20 '13 at 19:46
  • Thanks for your reply. Can you please explain with an example? @323go – user1741274 Jan 20 '13 at 19:52
  • There are plenty of examples online -- Google for it and come back here to ask a more specific question when you run into problems. SO is really meant to help, not to provide complete solutions. – 323go Jan 20 '13 at 20:17

3 Answers3

2

You may use Serializable interface as below:

// pass:
intent.putExtra("MotherClass", obj);

// receive:
getIntent().getSerializableExtra("MotherClass");

or even better, use Parcelable as below (sourced from @BomberMan's answer and this blog):

public class CustomObject1 implements Parcelable {

   // parcelable code CustomObject1
}
public class CustomObject2 implements Parcelable {

    private CustomObject1 obj1;

   // add  CustomObject1 here with getter setter
   // parcelable code for CustomObject2 

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(obj1, flags);
    }  
    private void readFromParcel(Parcel in) {
        obj1 = in.readParcelable(CustomObject1.class.getClassLoader());
    }
  ............
} 
Community
  • 1
  • 1
melvynkim
  • 1,655
  • 3
  • 25
  • 38
  • Thanks for your reply. The link was very useful for Parcelable. But can I also pass HttpResponse? (See my updated question). – user1741274 Jan 21 '13 at 08:22
1

In order to pass object as an extra. You may neen to implement Parclable or Serializable. But the default implementation of HttpResponse doesn't implement both. So what you need is to extend BasicHttpResponse and implement one of them.

wtsang02
  • 18,603
  • 10
  • 49
  • 67
  • I pass the HttpResponse as an extra in a hashMap. However, I get an exception like http://stackoverflow.com/questions/3818745/androidruntime-error-parcel-unable-to-marshal-value. Can you please explain how to resolve this issue? Thanks! @wtsang02 – user1741274 Jan 21 '13 at 10:43
0

I used a

HashMap<Object,Object> requestValue.put(response,"httpResponse"),

where response is the HttpResponse object and httpResponse is the string that serves as an ID for this request. I passed the String between my Activity and the service. Later, retrieved the value of the object (key) using its value (the String value). HttpResponse does not implement Serializable and hence this worked :) Thanks everyone for your replies.

user1741274
  • 759
  • 3
  • 13
  • 25