Java获取顺丰物流信息

顺丰物流是中国领先的快递物流服务提供商,为了方便开发者使用其物流信息查询功能,顺丰物流提供了一套API供开发者调用。本文将介绍如何使用Java语言调用顺丰物流API获取物流信息,并提供了相应的代码示例。

1. 准备工作

在开始之前,我们需要准备以下几个工作:

  1. 注册顺丰开放平台账号:访问[顺丰开放平台](

  2. 安装Java开发环境:确保你已经安装了Java开发环境,并配置好了相应的环境变量。

  3. 下载相关依赖:我们将使用Apache HttpClient库来发送HTTP请求,你可以在Maven或Gradle中添加以下依赖:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.13</version>
</dependency>

2. 调用顺丰物流API

顺丰物流提供了多个API供开发者查询不同类型的物流信息,例如查询运单轨迹、查询运费等。下面我们将以查询运单轨迹为例,介绍如何调用顺丰物流API。

2.1 生成签名

在调用顺丰物流API之前,我们需要先生成签名。签名是用来验证请求的合法性的,顺丰开放平台要求每个请求都要带上签名参数。

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SignUtils {
    public static String generateSignature(String appKey, String appSecret, long timestamp) {
        String rawString = appKey + appSecret + timestamp;
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] digest = md5.digest(rawString.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b & 0xff));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
}

2.2 发送请求

接下来,我们将使用HttpClient库发送HTTP请求到顺丰物流API,并获取物流信息的响应。

import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class SfExpressApi {
    private static final String API_URL = "

    public static String getTrackingInfo(String waybillNumber, String appKey, String appSecret) {
        long timestamp = System.currentTimeMillis();
        String signature = SignUtils.generateSignature(appKey, appSecret, timestamp);

        HttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(API_URL);
        httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8");

        String requestBody = String.format("{\"waybillNumber\":\"%s\",\"appKey\":\"%s\",\"timestamp\":%d,\"signature\":\"%s\"}",
                waybillNumber, appKey, timestamp, signature);
        httpPost.setEntity(new StringEntity(requestBody, StandardCharsets.UTF_8));

        try {
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            String responseString = EntityUtils.toString(entity, StandardCharsets.UTF_8);
            return responseString;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

2.3 解析响应

顺丰物流API的响应是一个JSON字符串,我们可以使用任何JSON解析库来解析响应。这里我们以Google Gson库为例,演示如何解析响应并获取物流信息。

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public class TrackingInfo {
    private String waybillNumber;
    private String status;
    private String origin;
    private String destination;
    // ...

    public static TrackingInfo fromJsonString(String