java client http请求 返回数据 实时循环监听 url 中资源是否生成

发布于:2024-12-19 ⋅ 阅读:(10) ⋅ 点赞:(0)

1、php 中 执行 exec 调用操作系统 命令行 执行 以下 java 代码 生成 的jar
2、php 执行命令是 以上1 需要命令行 输入 参数 taskid
3、实现实时监听 MP3 url 是否生成
4、

package com.example.filedemo.controller;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONObject;

public class MusicGeneration {
    public static void main(String[] args) {
        while (true) {
            try {
                String  taskId = "26516638-52fe-4973-9e84-886704c4dab7";
                URL url = new URL("https://api.openxs.top/suno/fetch/" + taskId);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setRequestProperty("Authorization", "your key ");
                connection.setRequestProperty("Accept", "application/json");
                connection.setRequestProperty("Content-Type", "application/json");
                connection.setInstanceFollowRedirects(true);
                connection.setConnectTimeout(0);
                connection.setReadTimeout(0);

                int responseCode = connection.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    String inputLine;
                    StringBuilder response = new StringBuilder();
                    while ((inputLine = in.readLine())!= null) {
                        response.append(inputLine);
                    }
                    in.close();

                    if (response.toString().isEmpty()) {
                        // 返回"创建失败"
                        System.out.println("创建失败");
                    } else {
                        JSONObject data = new JSONObject(response.toString());
                        if (data.isNull("data") || data.getJSONObject("data").isNull("status")) {
                            // 处理 status 为空的情况
                        } else if (data.getJSONObject("data").getString("status").equals("SUCCESS")) {
                            // 在这里进行数据库更新操作,此处仅为示例,实际中需要根据你的数据库操作方式进行修改
                            // 假设存在一个方法 updateDatabase 来执行数据库更新操作
                            updateDatabase(taskId, data.getJSONObject("data").getJSONArray("data").getJSONObject(0).getString("audio_url"),
                                    "已完成", data.getJSONObject("data").getJSONArray("data").getJSONObject(0).getString("image_url"));
                            break;
                        }
                    }
                } else {
                    // 处理错误响应
                    System.out.println("请求失败,响应代码: " + responseCode);
                }
                connection.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void updateDatabase(String taskId, String songId, String musicGenerationState, String musicImg) {
        // 在此处实现数据库更新操作
        System.out.println("更新数据库: taskId = " + taskId + ", songId = " + songId + ", musicGenerationState = " + musicGenerationState + ", musicImg = " + musicImg);
    }
}