增加运维的功能
parent
651af4dfe2
commit
85dd793fea
@ -0,0 +1,101 @@
|
|||||||
|
package com.xypower.common;
|
||||||
|
|
||||||
|
import android.text.TextUtils;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.Closeable;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
public class HttpRequest {
|
||||||
|
public static String get(String urlString) {
|
||||||
|
File downloadFile = null;
|
||||||
|
if (TextUtils.isEmpty(urlString))
|
||||||
|
return "";
|
||||||
|
HttpURLConnection connection = null;
|
||||||
|
StringBuilder response = new StringBuilder();
|
||||||
|
InputStream inputStream = null;
|
||||||
|
InputStreamReader isr = null;
|
||||||
|
BufferedReader br = null;
|
||||||
|
try {
|
||||||
|
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
|
||||||
|
URL url = new URL(urlString);
|
||||||
|
connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setConnectTimeout(5000);
|
||||||
|
connection.setReadTimeout(60000);
|
||||||
|
connection.setDoInput(true);
|
||||||
|
|
||||||
|
int responseCode = connection.getResponseCode();
|
||||||
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||||
|
inputStream = connection.getInputStream();
|
||||||
|
isr = new InputStreamReader(inputStream);
|
||||||
|
br = new BufferedReader(isr);
|
||||||
|
|
||||||
|
String line;
|
||||||
|
while ((line = br.readLine()) != null) {
|
||||||
|
response.append(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (connection != null)
|
||||||
|
connection.disconnect();
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (br != null) {
|
||||||
|
br.close();
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (isr != null) {
|
||||||
|
isr.close();
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (inputStream != null) {
|
||||||
|
inputStream.close();
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static final void closeSilently(Object closeable) {
|
||||||
|
try {
|
||||||
|
if (closeable != null) {
|
||||||
|
if (closeable instanceof Closeable) {
|
||||||
|
((Closeable) closeable).close();
|
||||||
|
} else if (closeable instanceof Socket) {
|
||||||
|
((Socket) closeable).close();
|
||||||
|
} else if (closeable instanceof ServerSocket) {
|
||||||
|
((ServerSocket) closeable).close();
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("Unknown object to close");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue