如何利用百度短链接接口将一个长链接变成短链接

本文介绍如何使用百度短网址服务API将长网址转换为短网址,包括代码实现、依赖添加及示例运行步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

短网址服务可以帮助你把一个长网址缩短,方便你在社交网络和第三方平台上分享链接,投放广告等等。 我们提供超简单的方式使用短网址服务:访问百度短网址首页https://dwz.cn,输入你要缩短的原网址,生成对应的短网址。你还可以调用百度短网址服务API服务,查看数据统计与分析……更多功能,等你来探索!

官方文档:https://dwz.cn/console/apidoc
首先添加依赖

<dependency>
 <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.8.5</version>
</dependency>

然后去百度官方文档https://dwz.cn/console/apidoc复制示例

package com.imooc.myo2o.util.baidu;

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

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;

public class BaiduDwz {
    final static String CREATE_API = "https://dwz.cn/admin/v2/create";
    final static String TOKEN = "你的token"; // TODO:设置Token

    class UrlResponse {
        @SerializedName("Code")
        private int code;

        @SerializedName("ErrMsg")
        private String errMsg;

        @SerializedName("LongUrl")
        private String longUrl;

        @SerializedName("ShortUrl")
        private String shortUrl;

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getErrMsg() {
            return errMsg;
        }

        public void setErrMsg(String errMsg) {
            this.errMsg = errMsg;
        }

        public String getLongUrl() {
            return longUrl;
        }

        public void setLongUrl(String longUrl) {
            this.longUrl = longUrl;
        }

        public String getShortUrl() {
            return shortUrl;
        }

        public void setShortUrl(String shortUrl) {
            this.shortUrl = shortUrl;
        }
    }

    /**
     * 创建短网址
     *
     * @param longUrl
     *            长网址:即原网址
     * @return  成功:短网址
     *          失败:返回空字符串
     */
    public static String createShortUrl(String longUrl) {
        String params = "{\"url\":\""+ longUrl + "\"}";

        BufferedReader reader = null;
        try {
            // 创建连接
            URL url = new URL(CREATE_API);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestMethod("POST"); // 设置请求方式
            connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
            connection.setRequestProperty("Token", TOKEN); // 设置发送数据的格式");

            // 发起请求
            connection.connect();
            OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码
            out.append(params);
            out.flush();
            out.close();

            // 读取响应
            reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String line;
            String res = "";
            while ((line = reader.readLine()) != null) {
                res += line;
            }
            reader.close();

            // 抽取生成短网址
            UrlResponse urlResponse = new Gson().fromJson(res, UrlResponse.class);
            if (urlResponse.getCode() == 0) {
                return urlResponse.getShortUrl();
            } else {
                System.out.println(urlResponse.getErrMsg());
            }

            return ""; // TODO:自定义错误信息
        } catch (IOException e) {
            // TODO
            e.printStackTrace();
        }
        return ""; // TODO:自定义错误信息
    }

    public static void main(String[] args) {
        String res = createShortUrl("你的长网址");
        System.out.println(res);

    }
}

把代码中的token换成你的token
在官方文档https://dwz.cn/console/apidoc中获取token
在这里插入图片描述
在这里插入图片描述
到此就ok了,可以在main方法中试一下效果

Rebuild started: Project: Project *** Using Compiler 'V6.22', folder: 'E:\Keil_v5\ARM\ARMCLANG\Bin' Rebuild target 'Target 1' assembling startup_stm32f10x_md.s... Start/core_cm3.c(445): error: non-ASM statement in naked function is not supported 445 | uint32_t result=0; | ^ Start/core_cm3.c(442): note: attribute is here 442 | uint32_t __get_PSP(void) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(465): error: parameter references not allowed in naked functions 465 | "BX lr \n\t" : : "r" (topOfProcStack) ); | ^ Start/core_cm3.c(461): note: attribute is here 461 | void __set_PSP(uint32_t topOfProcStack) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(479): error: non-ASM statement in naked function is not supported 479 | uint32_t result=0; | ^ Start/core_cm3.c(476): note: attribute is here 476 | uint32_t __get_MSP(void) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(499): error: parameter references not allowed in naked functions 499 | "BX lr \n\t" : : "r" (topOfMainStack) ); | ^ Start/core_cm3.c(495): note: attribute is here 495 | void __set_MSP(uint32_t topOfMainStack) __attribute__( ( naked ) ); | ^ 4 errors generated. compiling core_cm3.c... compiling misc.c... compiling system_stm32f10x.c... compiling stm32f10x_adc.c... compiling stm32f10x_dac.c... compiling stm32f10x_exti.c... compiling stm32f10x_dbgmcu.c... compiling stm32f10x_dma.c... compiling stm32f10x_crc.c... compiling stm32f10x_cec.c... compiling stm32f10x_bkp.c... compiling stm32f10x_can.c... compiling stm32f10x_flash.c... compiling stm32f10x_pwr.c... compiling stm32f10x_fsmc.c... compiling stm32f10x_
03-31
<think>嗯,用户遇到了Keil V6.22在编译STM32F10x项目时,core_cm3.c文件中出现裸函数中的非汇编语句和参数引用错误。我需要仔细分析这个问题,并给出解决方案。 首先,根据用户提供的引用资料,问题似乎是由于Keil的编译器版本不兼容引起的。引用[3]提到,江科大的启动文件使用的是版本5的编译器,而新版的MDK默认使用版本6,导致不兼容。用户现在用的是V6.22,可能默认也是版本6编译器,而core_cm3.c中的裸函数可能包含非汇编语句,这在版本6中不被支持。 裸函数(naked function)通常用于直接编写汇编代码,不允许有C语句,否则编译器会报错。版本6可能对此检查更严格。引用[4]指出,将编译器版本切换回5可以解决这个问题。但用户可能不想降级,或者最新版MDK已经没有版本5选项,这时候可能需要修改项目设置或调整代码。 另一个解决方法是修改core_cm3.c文件中的相关函数。例如,检查报错的445行,是否在裸函数中使用了C语句。可能需要将其替换为纯汇编代码,或者将函数声明为非naked。不过,修改库文件需谨慎,可能影响其他部分。 用户可能需要指导他们如何切换编译器版本到5,如果安装包中没有,可能需要单独安装旧版本编译器,或者从Keil官网下载。同时,检查项目配置中的编译器选项是否正确设置为版本5。 另外,引用[2]提到可以不用替换文件,而是选择使用编译器版本5,这需要用户安装MDK版本5并配置项目使用它。但用户可能已经安装了较新版本,所以可能需要调整项目设置而非整个MDK版本。 总结可能的解决步骤:1. 检查并切换项目中的编译器版本到5;2. 修改core_cm3.c中的裸函数,移除非汇编语句;3. 如果无法切换编译器,考虑更新固件库到兼容版本6的版本。 需要确保回答结构清晰,分步骤说明,并提供相关引用支持。同时,生成相关问题,帮助用户进一步理解可能遇到的问题。</think>### 解决Keil V6.22编译core_cm3.c的裸函数错误 #### 问题分析 1. **根本原因**:Keil MDK默认使用Compiler V6,而STM32F10x的固件库(如`core_cm3.c`)可能仅兼容Compiler V5,导致裸函数(`naked function`)中出现非汇编语句或参数引用错误[^3][^4]。 - 裸函数要求仅包含汇编指令,V6编译器对此检查更严格。 - 编译器版本不兼容时,原有代码中的C语句或参数操作会触发报错。 --- #### 解决方案 ##### 方法1:切换为Compiler V5 1. **安装Compiler V5**(若未预装): - 下载并安装**Legacy Support for ARM Compiler 5**(需Keil官网账户)[^2]。 2. **配置项目使用V5**: - 在Keil中打开项目,进入`Project -> Options for Target -> Target`选项卡。 - 将`ARM Compiler`选项从`V6.22`改为`Use default compiler version 5`。 3. **重新编译**:验证是否解决报错。 ##### 方法2:修改core_cm3.c的裸函数(高风险) 1. **定位错误位置**: - 找到报错行(如`core_cm3.c`第445行),检查裸函数`__ASM void ...`中的非汇编语句。 2. **重写为纯汇编**: - 将C语句替换为内联汇编,例如: ```c __ASM void FunctionName(...) { // 替换为汇编指令,如MOV、BX LR等 } ``` 3. **注意**:此操作需熟悉ARM汇编,且可能影响库功能,建议优先切换编译器。 ##### 方法3:更新固件库(推荐) 1. **下载兼容V6的库**: - 从STM官网或CubeMX获取最新`CMSIS`包,确保支持Compiler V6。 2. **替换旧文件**: - 将项目中的`core_cm3.c`、`startup_stm32f10x_xx.s`等替换为新版本文件。 3. **验证兼容性**:重新编译项目,检查是否消除报错。 --- #### 扩展建议 - **兼容性检查**:若使用Keil V6.22,建议逐步迁移到官方支持的库版本,避免长期依赖旧编译器。 - **调试技巧**:若报错涉及参数引用,检查裸函数是否直接操作了C语言参数(如`int x`),需改用寄存器传递(如`R0`)。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值