httpurlconnection get String

public static String getJSON(String url, int timeout) {
HttpURLConnection c = null;
try {
long requestStart = System.currentTimeMillis();
URL u = new URL(url);
c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setRequestProperty("Content-length", "0");
c.setUseCaches(false);
c.setAllowUserInteraction(false);
c.setConnectTimeout(timeout);
c.setReadTimeout(timeout);
c.connect();
int status = c.getResponseCode();

switch (status) {
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
br.close();
return sb.toString();
}
long requestStop = System.currentTimeMillis();
StatisticsUtil.networkEvent(SysConstant.CONVERT_TOKEN_HOST,
status,
requestStart, requestStop,
StatisticsUtil.calRequestSize(url, mapHeaders),
StatisticsUtil.calResponseSize(httpResponse, result),
statusCode, serverMessage);

} catch (MalformedURLException ex) {
} catch (IOException ex) {
} finally {
if (c != null) {
try {
c.disconnect();

} catch (Exception ex) {
}
}
}
return null;
}

你可能感兴趣的:(httpurlconnection get String)