在获取到自己的信息后
一直调用头像url 却获取不了!原来是要在后面加上头像的大小 如getHead(head+"/50");
下面给出获取头像的代码
/**
* 获取头像的Demo代码
* @param head 注意加头像大小 /50 /100
* @throws MalformedURLException
* @throws IOException
*/
private static void getHead(String head) throws MalformedURLException, IOException {
System.out.println(head);
HttpURLConnection hConnection=(HttpURLConnection) new URL(head).openConnection();
hConnection.setRequestMethod("GET");
System.out.println(hConnection.getResponseCode());
if (hConnection.getResponseCode()==200) {
InputStream inputStream=hConnection.getInputStream();
byte[] data=readInputStream(inputStream);
FileOutputStream fileOutputStream=new FileOutputStream("C://12.jpg");
fileOutputStream.write(data);
fileOutputStream.close();
}
}
private static byte[] readInputStream(InputStream inputStream) throws IOException {
// TODO Auto-generated method stub
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int lenght=-1;
while((lenght=inputStream.read(buffer))!=-1){
byteArrayOutputStream.write(buffer, 0, lenght);
}
byteArrayOutputStream.close();
inputStream.close();
return byteArrayOutputStream.toByteArray();
}
参考于http://www.dengguibao.cn/blog/110
关于腾讯微博API的几个注意事项
发表于 2011-01-15 18:02:33