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?
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)
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.
Send data to server using AsyncTask
After getting result in doInBackground , continue with onPostexecute()
- Start the next activity in onPostexecute()
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 :-)
- Send data to server
Get the response
Put that string as Extra in Intent and start activity
Intent i = new Intent(this, YourNextActivityName.class);
i.putExtra("res",response.toString);
i.startActivity();On Next Activity get that Data and display.
Bundle extras = getIntent().getExtras(); if (extras != null) { String response= extras.getString("res"); }
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.
© 2022 - 2024 — McMap. All rights reserved.
json
, you can send direct asjson.toString()
– Opine