在Java中,獲取QQ號(hào)對(duì)應(yīng)的頭像和昵稱,需要借助QQ官方提供的API接口來實(shí)現(xiàn)。下面是一個(gè)簡單的示例代碼:
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class QQInfoUtil { public static void main(String[] args) { String qq = "123456"; //需要獲取的QQ號(hào) try { URL url = new URL("http://r.qzone.qq.com/fcg-bin/cgi_get_portrait.fcg?uins=" + qq); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "GBK")); String line; StringBuilder sb = new StringBuilder(); while((line = br.readLine()) != null) { sb.append(line); } br.close(); conn.disconnect(); String resultJson = sb.toString(); String nickname = resultJson.split("\"")[1]; String avatarUrl = "http://q.qlogo.cn/g?b=qq&nk=" + qq + "&s=640"; System.out.println("QQ號(hào):" + qq); System.out.println("昵稱:" + nickname); System.out.println("頭像URL:" + avatarUrl); } catch (Exception e) { e.printStackTrace(); } } }
上述代碼中,通過訪問QQ官方提供的API接口,獲取到了對(duì)應(yīng)QQ號(hào)的頭像和昵稱信息,并輸出到控制臺(tái)上。其中,頭像URL的生成方式為:http://q.qlogo.cn/g?b=qq&nk=QQ號(hào)&s=SIZE,其中SIZE為頭像尺寸,可以根據(jù)需要調(diào)整。例如:s=640代表640*640的頭像。