After sending data to server how to print response in next activity?
Asked Answered
P

6

8

I am trying to send data to server and in response i am getting some data which i need to display in next activity using listview,can any one tell me how to do this?

Percussionist answered 5/12, 2014 at 10:7 Comment(6)
use shared prefrences or a singleton classCaceres
what type of data you have to pass like : String,Object,List etc ?Hirohito
i am sending data to server in text and getting response in textPercussionist
In which format you are getting data? If json, you can send direct as json.toString()Opine
You said you are sending text to server and getting text in response, that you want to send to next activity. So what about images now?Bitstock
this question is asked many times in many different ways, please consider doing a search.Materfamilias
S
3

use bundle to move your response from one activity to another

-create class that implements Parcelable

  public class ResponseData implements Parcelable{}

-Next save it in intent:

intent.putExtra(key, new ResponseData(someDataFromServer));

-Last step - retrive it:

Bundle data = getIntent().getExtras();
ResponseData response= (ResponseData ) data.getParcelable(key);

After that add it to an adapter to display it for the user

In other case you can save it in application context or database (not recommended)

Studhorse answered 5/12, 2014 at 10:12 Comment(0)
Y
1

I would strongly suggest you to pass necessary parameters to next activity using Bundle and from next activity itself you should make a call to server to receive and display necessary information.

Yellowknife answered 5/12, 2014 at 10:15 Comment(0)
R
0
  • Send data to server using AsyncTask

  • After getting result in doInBackground , continue with onPostexecute()

  • Start the next activity in onPostexecute()
Reiss answered 5/12, 2014 at 10:10 Comment(2)
ok i am sending data using some fields like country,mother tongue,religion.it is kind of searching users list..and in response i will get user's name in list which i need to show in next activity.Percussionist
#7578736 Is it ok for you ?Reiss
A
0

Try using an event bus library, such as greenrobot EventBus. With it, the process will be done in 4 lines of code:

(In sender Activity)

EventBus.getDefault().register(this);
EventBus.getDeafult().postSticky(myDataObject);

(In receiving Activity)

DataObject passedData = EventBus.getDefault().getStickyEvent(DataObject.class);

You will also need a POJO data class:

public static class DataObject{
    private String data;

    public String getData(){
        return data;
    }

    public void setData(String data){
        this.data = data;
    }

}

Clean, quick and elegant :-)

Anastomosis answered 15/12, 2014 at 11:34 Comment(0)
P
0
  1. Send data to server
  2. Get the response

  3. Put that string as Extra in Intent and start activity

    Intent i = new Intent(this, YourNextActivityName.class);
    i.putExtra("res",response.toString);
    i.startActivity();

  4. On Next Activity get that Data and display.

     Bundle extras = getIntent().getExtras();
     if (extras != null) {
        String response= extras.getString("res");
    }
    
Perchloride answered 16/12, 2014 at 5:56 Comment(0)
J
0

Steps:

 1)Send data.
 2)Get Response
 3)if respose is ok, save that in a static string and fire intent and on the next 
   activity get the respose using classname.your string. simple and best way.
Jeanene answered 16/12, 2014 at 6:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.