package com.example.duomimusic.utils;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.util.ByteArrayBuffer;
public class HttpUtils
{
public static String downloadJson(URL... params)
{
//得到url网址
URL url = params[0];
InputStream is = null;
ByteArrayBuffer strBuf = new ByteArrayBuffer(3000);
try
{
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置服务器连接超时时间
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
//判断服务器是否正确响应
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK)
{
is = conn.getInputStream();
byte[] buffer = new byte[1024];
int length = 0;
while (-1 != (length = is.read(buffer)))
{
strBuf.append(buffer, 0, length);
}
}
else
{
return null;
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (null != is)
{
try
{
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return new String(strBuf.buffer(), 0 ,strBuf.length() );
}
}