Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用try块在Android中打印JSON对象_Android_Mysql_Json - Fatal编程技术网

如何使用try块在Android中打印JSON对象

如何使用try块在Android中打印JSON对象,android,mysql,json,Android,Mysql,Json,我需要从MySQL数据库中获取JSON对象,并将其打印为字符串。另外,我需要将该字符串传递给try块以打开所需的类活动。在这里,我已经附上我的代码下面,检查我的代码,让我知道 我的JSON对象示例: { "id": "381", "task": "user_confirm", "message": "New booking details" } 我的Android代码: public void onMessageReceived(final RemoteMessage remoteMessag

我需要从MySQL数据库中获取JSON对象,并将其打印为字符串。另外,我需要将该字符串传递给try块以打开所需的类活动。在这里,我已经附上我的代码下面,检查我的代码,让我知道

我的JSON对象示例:

{ "id": "381", "task": "user_confirm", "message": "New booking details" }
我的Android代码:

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,任务:用户\确认,消息:新预订详细信息}

根据附加的JSON响应,JSON数据中没有JSONObject命名数据

按如下方式解析JSON数据:

希望这能起作用~

像这样使用FCM:

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

php标记和mysql标记没有关系。。。更改为添加json标记检查我的更新答案。使用JSONObject json=new JSONObject RemoteMessage.getData.toString;而不是JSONObject json=new JSONObject RemoteMessage.getData;私有void sendNotificationJSONObject json{Log.eTAG,通知JSON1+json.toString;尝试{id=json.getStringid;message=json.getStringmessage;task=json.getStringtask;Log.dId值,id;Log.dMessage,message;Log.dtask,task;}
if (remoteMessage.getData().size() > 0) {
        String message=remoteMessage.getData().get("message");
}