百度的短地址生成好像挂掉了
来源:14-11 店铺授权之短链接的实现
vvshyer
2018-02-03
直接访问dwz.cn生成短地址时 返回空白
在代码直接测试生成短地址返回response string: {"status":-1,"err_msg":"","longurl":""}
写回答
1回答
-
同学好,针对同学的问题,翔仔去调研了下,确实是百度短链接服务崩了,会试着去反馈下这个问题,同学可以等待其修复完成,或者替换一下翔仔临时优化的一段代码,换了另外一个服务,照样能用:),只需要把短链接工具util替换成如下代码并添加两个jar包即可:
pom.xml<dependencies>标签里加入如下两个jar
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.45</version> </dependency> <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp --> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.9.1</version> </dependency>
ShortNetAddressUtil.java替换成如下代码
package com.imooc.o2o.util; import java.io.IOException; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class ShortNetAddressUtil { public static String getShortURL(String longUrl) { try { String result = callHttp("http://suo.im/api.php?format=json&url=" + longUrl); JSONObject jsonResult = JSON.parseObject(result); return jsonResult.getString("url"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } private static String callHttp(String requestUrl) throws IOException { OkHttpClient httpClient = new OkHttpClient(); // 创建OkHttpClient对象 Request request = new Request.Builder().url(requestUrl)// 请求接口。如果需要传参拼接到接口后面。 .build();// 创建Request 对象 Response response = null; response = httpClient.newCall(request).execute();// 得到Response 对象 return response.body().string(); } /** * 百度短链接接口 * * @param args */ public static void main(String[] args) { System.out.println(getShortURL("https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login")); } }
PS:不好意思,昨天聚会喝断片了今天才起来,头晕乎乎的:)
032018-05-09
相似问题