title: How to print the JSON object in Android using a try block
body: <p>I need to get the JSON object from MySQL database, and to print it as string. Also, I need to pass that string to a <code>try</code> block to open required class activity. Here, I have attached my code below, check my code and let me know.</p>
  
  <p>My JSON object sample:</p>
  
  <pre><code>{ "id": "381", "task": "user_confirm", "message": "New booking details" }
  </code></pre>
  
  <p>My Android Code:</p>
  
  <pre><code>public void onMessageReceived(final RemoteMessage remoteMessage) {
      Intent intent = new Intent(this, Login_Activity.class);
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
      NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
      notificationBuilder.setContentTitle(getResources().getString(R.string.app_name));
      notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
      notificationBuilder.setAutoCancel(true);
      notificationBuilder.setSmallIcon(R.mipmap.main_logo);
      notificationBuilder.setContentIntent(pendingIntent);
  
      Uri path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.bike_start);
      notificationBuilder.setSound(path);
      NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
      notificationManager.notify(0,notificationBuilder.build());
  
      if (remoteMessage.getData().size() &gt; 0) {
          try {
              JSONObject json = new JSONObject(remoteMessage.getData());
              Log.e("JsonOutput", json.toString());
              sendNotification(json);
          } catch (Exception e) {
              Log.e(TAG, "Exception: " + e.getMessage());
          }
      }
      if (remoteMessage.getNotification()!=null){
          Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
      }
  
      //        sendNotification1(String.valueOf(remoteMessage), image);
      // Log.e("body", body);
  }
  
  private void sendNotification(JSONObject json) {
      Log.e(TAG, "Notifications JSON1 " + json.toString());
  
      try {
           JSONObject data = json.getJSONObject("data");
           Log.d("data", String.valueOf(data));
           //parsing json data
           id = data.get("id").toString();
           message = data.get("message").toString();
           task = data.get("task").toString();
           Log.d("Id values", id);
           Log.d("Message", message);
      }
  }
  </code></pre>
  
  <p>Output:</p>
  
  <blockquote>
    <p>Notifications JSON1 <code>{"id":"381","task":"user_confirm","message":"New booking details"}</code></p>
  </blockquote>