@Test
public void justTest() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("HEHEHEHEHEHEHEH");
}
});
thread.start();
}
public interface LoaderFromNet {
public void loadFromNetDone (final String object);
}
public interface LoadingDataFromNet {
public void loadingDataFromNet (final String object);
}
/**
* Base Net class for download data from internet
*/
public class Net implements LoadingDataFromNet {
private final String log_Net = "log_Net";
private LoaderFromNet loaderFromNet;
private String url;
/**
* Public constructor sets main parameters for downloading json string
*
* @param loaderFromNet - Interface
* @param url - website's address
*/
public Net(LoaderFromNet loaderFromNet, String url) {
this.loaderFromNet = loaderFromNet;
this.url = url;
execute();
}
private void execute() {
if (loaderFromNet == null)
throw new NullPointerException("Loader interface is null!");
if (url == null)
throw new NullPointerException("URL is null!");
new Loading(this, url);
}
@Override
public void loadingDataFromNet(String object) {
loaderFromNet.loadFromNetDone(object);
}
}
public class Loading implements Runnable {
private LoadingDataFromNet loadingDataFromNet;
private String url;
protected Loading(LoadingDataFromNet loadingDataFromNet, String url) {
this.loadingDataFromNet = loadingDataFromNet;
this.url = url;
execute();
}
private void execute() {
new Thread(this).start();
}
@Override
public void run() {
try {
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) new URL(url).openConnection();
httpsURLConnection.setRequestMethod("GET");
httpsURLConnection.connect();
int responseCode = httpsURLConnection.getResponseCode();
httpsURLConnection.disconnect();
if (responseCode == 200){
InputStream inputStream = new URL(url).openStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
int letter = -1;
StringBuilder stringBuilder = new StringBuilder();
while ((letter = bufferedReader.read()) != -1)
stringBuilder.append((char) letter);
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
loadingDataFromNet.loadingDataFromNet(stringBuilder.toString());
} else {
loadingDataFromNet.loadingDataFromNet(null);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Base Net class for download data from internet
*/
public class Net implements LoadingDataFromNet {
private final String log_Net = "log_Net";
private LoaderFromNet loaderFromNet;
private String url;
private Loading loading;
/**
* Public constructor sets main parameters for downloading json string
*
* @param loaderFromNet - Interface
* @param url - website's address
*/
public Net(LoaderFromNet loaderFromNet, String url) {
this.loaderFromNet = loaderFromNet;
this.url = url;
execute();
}
private void execute() {
if (loaderFromNet == null)
throw new NullPointerException("Loader interface is null!");
if (url == null)
throw new NullPointerException("URL is null!");
loading = new Loading(this, url);
}
@Override
public void loadingDataFromNet(String object) {
loaderFromNet.loadFromNetDone(object);
}
}
public class Loading implements Runnable {
private LoadingDataFromNet loadingDataFromNet;
private String url;
protected Loading(LoadingDataFromNet loadingDataFromNet, String url) {
this.loadingDataFromNet = loadingDataFromNet;
this.url = url;
execute();
}
private void execute() {
new Thread(this).start();
}
@Override
public void run() {
try {
URL mURL = new URL(url);
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) mURL.openConnection();
httpsURLConnection.setRequestMethod("GET");
httpsURLConnection.connect();
int responseCode = httpsURLConnection.getResponseCode();
httpsURLConnection.disconnect();
if (responseCode == 200){
InputStream inputStream = mURL.openStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
int letter = -1;
StringBuilder stringBuilder = new StringBuilder();
while ((letter = bufferedReader.read()) != -1)
stringBuilder.append((char) letter);
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
loadingDataFromNet.loadingDataFromNet(stringBuilder.toString());
} else {
loadingDataFromNet.loadingDataFromNet(null);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}