4

How to use putExtra to get JsonObject? I want to get JSON text to display another activity.

 @Override
 protected String doInBackground(Void... v) {

    HashMap<String, String> params = new HashMap<>();

    params.put(KEY_MEMBER_ID, memberId);
    params.put(KEY_PWD, pwd);

    JSONParser jParser = new JSONParser();

    JSONObject json = jParser.sendPostRequest(GlobalVariabel.URL_LOGIN_MEMBER, params);
    System.out.println("URL :######### "+GlobalVariabel.URL_LOGIN_MEMBER);

    try {
        status = json.getString("STATUS");

        System.out.println("STATUSSSSSSSSSSSSSSSSSS : " + status);
        System.out.println(json);

        JSONObject jObj = json.getJSONObject("PROFILE");
        if (jObj != null) {
            user_id = jObj.getString("USER_ID");
            profile_url = jObj.getString("PROFILE_URL");
            date_joined = jObj.getString("DATE_JOINED");
            phone_type = jObj.getString("PHONE_TYPE");
            email = jObj.getString("EMAIL");
            name = jObj.getString("NAME");
        }

        } catch (JSONException e) {
            e.printStackTrace();
        }
            return null;
        }

I want get JsonObject email and name use intent.putExtra to another activity.

Intent intent = new Intent(getActivity(), MainActivity.class);
                    intent.putExtra(".... ", ....);
                    //intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);
Driyanto Saputro
  • 103
  • 1
  • 2
  • 9

4 Answers4

8

For sending the json

Intent intent = new Intent(getActivity(), MainActivity.class);
                intent.putExtra("json", jsonObject.toString());
                startActivity(intent);

For retrieving

       if(getIntent().hasExtra("json")) { 
       JsonObject mJsonObject = new JsonObject(getIntent().getStringExtra("json"));
    }
Selvin
  • 6,598
  • 3
  • 37
  • 43
Zubair Soomro
  • 180
  • 1
  • 12
1

The json is in fact a "String" text with a determined format.

First option:

I recommend you to pass it to String to send it by putExtra and then to re-map it to the json object:

Intent intent = new Intent(getActivity(), MainActivity.class);
                    intent.putExtra("JSON_OBJECT", json.toString());
                    //intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);

and retrieve it by:

String jsonObject = intent.getStringExtra("JSON_OBJECT");

After that, you will have to re-build the json object.

Second option:

Convert the jsonObject to a POJO object which implements Serializable or Parcelable and send it through:

intent.putExtra("POJO_OBJECT", myJsonObject);

and retrieve it by:

MyJsonObject myjsonObject = (MyJsonObject)intent.getParcelableExtra("POJO_OBJECT", myJsonObject);
zapotec
  • 2,628
  • 4
  • 31
  • 53
0

try this //decalre a global string as String JsonOutput="";

@Override
protected String doInBackground(Void... v) {

HashMap<String, String> params = new HashMap<>();

params.put(KEY_MEMBER_ID, memberId);
params.put(KEY_PWD, pwd);

JSONParser jParser = new JSONParser();

JSONObject json = jParser.sendPostRequest(GlobalVariabel.URL_LOGIN_MEMBER, params);
System.out.println("URL :######### "+GlobalVariabel.URL_LOGIN_MEMBER);

try {
    status = json.getString("STATUS");

    System.out.println("STATUSSSSSSSSSSSSSSSSSS : " + status);
    System.out.println(json);

    JSONObject jObj = json.getJSONObject("PROFILE");
    JsonOutput=jobj.toString(); //convert your json to string here.

    if (jObj != null) {
        user_id = jObj.getString("USER_ID");
        profile_url = jObj.getString("PROFILE_URL");
        date_joined = jObj.getString("DATE_JOINED");
        phone_type = jObj.getString("PHONE_TYPE");
        email = jObj.getString("EMAIL");
        name = jObj.getString("NAME");
    }

    } catch (JSONException e) {
        e.printStackTrace();
    }
        return null;
    }

in postExecute()

proctected void onPostExcute(){
            Intent intent = new Intent(yourActivity.this, MainActivity.class);
            intent.putExtra("YOUR_KEY ", JsonOutput);// result contains all    jsonObject value
            //intent.setFlags(intent.getFlags() |Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
   }

in MainActivity.class //where you want to get this details.

String Jsonoutput=getIntent().getExtras().getString("Your Key");
JSONObject job=new JSOObject(Jsonoutput);
//now parse this job to get your name and email.or anuy other data in jobj.

Hope this helps.

Rushi Ayyappa
  • 2,708
  • 2
  • 16
  • 32
-2

Just do like this

Go to onPostExcute() Method

proctected void onPostExcute(String result){
                Intent intent = new Intent(getActivity(), MainActivity.class);
                intent.putExtra("YOUR_KEY ", result);// result contains all jsonObject value
                //intent.setFlags(intent.getFlags() |Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);}

Hope this will help you. :)

Sanwal Singh
  • 1,765
  • 3
  • 17
  • 35