【问题标题】:How to print the JSON object in Android using a try block如何使用 try 块在 Android 中打印 JSON 对象
【发布时间】:2017-12-08 19:01:42
【问题描述】:

我需要从 MySQL 数据库中获取 JSON 对象,并将其打印为字符串。此外,我需要将该字符串传递给try 块以打开所需的课堂活动。在这里,我在下面附上了我的代码,请检查我的代码并告诉我。

我的 JSON 对象示例:

{ "id": "381", "task": "user_confirm", "message": "New booking details" }

我的安卓代码:

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() > 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);
    }
}

输出:

通知 JSON1 {"id":"381","task":"user_confirm","message":"New booking details"}

【问题讨论】:

  • php标签和mysql标签没有关系...改为添加json标签

标签: android mysql json


【解决方案1】:

根据您附加的JSON 回复,您的JSON 数据中没有名为"data"JSONObject

如下解析 JSON 数据:

if (remoteMessage.getData().size() > 0) {
    try {
        JSONObject json = new JSONObject(remoteMessage.getData().toString());
        Log.e("JsonOutput", json.toString());
        sendNotification(json);
    } catch (Exception e) {
        Log.e(TAG, "Exception: " + e.getMessage());
    }
}
...........
.......................

private void sendNotification(JSONObject json) {
    Log.e(TAG, "Notifications JSON1 " + json.toString());

    try {
         // Parsing id, task and message from json JSONObject 
         String id = json.getString("id");
         String task = json.getString("task");
         String message = json.getString("message");

         Log.d("Id: ", id);
         Log.d("Task: ", task);
         Log.d("Message: ", message);

    } catch (JSONException e) {
        Log.e(TAG, "Json Exception: " + e.getMessage());
    } catch (Exception e) {
        Log.e(TAG, "Exception: " + e.getMessage());
    }
}

希望这会奏效~

【讨论】:

  • 检查我更新的答案。使用JSONObject json = new JSONObject(remoteMessage.getData().toString()); 而不是JSONObject json = new JSONObject(remoteMessage.getData());
【解决方案2】:

像这样用于 FCM:

if (remoteMessage.getData().size() > 0) {
        String message=remoteMessage.getData().get("message");
}

【讨论】:

  • private void sendNotification(JSONObject json) { Log.e(TAG, "Notifications JSON1 " + json.toString());尝试 { id = json.getString("id");消息 = json.getString("消息");任务 = json.getString("任务"); Log.d("Id 值", id); Log.d("消息", 消息); Log.d("任务", 任务); }
猜你喜欢
  • 2016-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
  • 2013-06-21
  • 2017-12-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多