pawan kumar
pawan kumar

Reputation: 61

How to pass json result from one activity to another

I want to use json result to another activity.Here following code I want to use intents to pass the data second activity but I am not able to intents class.How can i use intent to pass the value

 if (status == 200) {
    HttpEntity entity = response.getEntity();
    String data = EntityUtils.toString(entity);
    System.out.println(data);



    System.out.println("fffff");
    JSONObject jsono = new JSONObject(data);
    JSONArray jarray = jsono.getJSONArray("articles");
    System.out.println(jarray);
    for (int i = 0; i < jarray.length(); i++) {
        JSONObject object = jarray.getJSONObject(i);

        Dashboard_Model_File actor = new Dashboard_Model_File();
        actor.setTitle(object.getString("category_name"));
        actor.setDescription(object.getString("bookmark_title"));
        actor.setUrl(object.getString("bookmark_website"));

        actor.setBookmark_id(object.getString("bookmark_id"));
        actor.setAlternate_id(object.getString("alternate_id"));
        actor.setBookmark_file(object.getString("bookmark_file"));
        actor.setMode(object.getString("mode"));

        actor.setImage(object.getString("bookmark_preview_image"));

        actorsList.add(actor);
    }
    return true;
}

Upvotes: 0

Views: 1951

Answers (4)

Jhaman Das
Jhaman Das

Reputation: 1114

Rather than using shared pereferences or some other tactics:

Simple and easy solution use Singleton patteren parse you json result and set and get the values, like this :

public class RecentRequest {

String recieverName;
String dateTime;
String receiverNumber;

public String getRecieverName() {
    return recieverName;
}

public void setRecieverName(String recieverName) {
    this.recieverName = recieverName;
}

public String getDateTime() {
    return dateTime;
}

public void setDateTime(String dateTime) {
    this.dateTime = dateTime;
}

public String getReceiverNumber() {
    return receiverNumber;
}

public void setReceiverNumber(String receiverNumber) {
    this.receiverNumber = receiverNumber;
}
}     

Please let me know if u need any other help!! Cheers !!

Upvotes: 0

Onkar Nene
Onkar Nene

Reputation: 1377

Try this -

Parse data in string

String categoryNameData = object.getString("category_name");  //Do this for all data object

Check if data is not null and then pass intent

if(categoryNameData != null){
Intent intent = new Intent("YourCurrentActivityName".this , "TargetActivityName".class);
intent.putExtra("cat" , categoryNameData);    //do this for all data objects
startActivity(intent);
}

In Target Activity

String category = getIntent.getStringExtra("cat");

Hope this will help :)

Upvotes: 0

Basil Battikhi
Basil Battikhi

Reputation: 2668

i recommended to send JSON result as string see the following code

Intent intent=new Intent(getApplicationContext(),DistClass.class);
intent.putExtra("json",object.toString());
startActivity(intent);

in the second class do the following

String jsonResult=getIntent().getExtras().getString("json");
JSONObject json=new JSONObject(jsonResult);

that's all

Upvotes: 1

Akshay Kulkarni
Akshay Kulkarni

Reputation: 729

You should read this here:

https://github.com/google/gson

same gson you can send to the other activity or store as the sharedPreference and access the shared prefs in another activity.

or there is a silly way to convert to string and send in intent with putExtras and access it in another activity as getExtras and parse again in the other activity.

Happy Coding

Upvotes: 0

Related Questions