Answer the question
In order to leave comments, you need to log in
Why is a String in Eclipse truncated when requested via a URL?
Hello!
I wanted to process the data contained when received on request through the URL library. But the data is not fully output both to the console and when saved to a file. I can't understand why? I work through Eclipse. I already solved a similar problem, but only on Idea (the trial license ended) the code worked. By the way, when I started the debug (Debug as) I saw that the data comes in full (5000 geo points in the form of a json array). Why is a string truncated when it is assigned to a variable and output to the console? Here is the code itself: (WARNING I use lombok!)
package com.jsonEx.il;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class UrlConnection {
private final String adress;
private final String PATH;
private URL url;
/**
* Метод получения соединения URL
*
* @return URL
*/
public URL getUrl() {
try {
url = new URL(adress);
System.out.println(url.toString());
} catch (MalformedURLException e) {
System.out.println("Ошибка UrlConnection в getUrl(): " + e);
}
return url;
}
/**
* Метод получения содержания запроса по url
*
* @return String
*/
public String getJsonString() {
StringBuffer strB1 = new StringBuffer();
getUrl();
try {
//HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
BufferedInputStream bufinstream = new BufferedInputStream(url.openConnection().getInputStream(), 64);
byte[] bytemas = new byte[64];
while (bufinstream.available() != 0) {
bytemas = bufinstream.readAllBytes();
strB1.append(new String(bytemas, StandardCharsets.UTF_8));
}
bufinstream.close();
} catch (IOException e) {
System.out.println("Ошибка UrlConnection в getJsonString(): ");
e.printStackTrace();
}
return strB1.toString();
}
public String saveInFiles(String json) {
Path path = Paths.get(PATH);
if (!(Files.exists(path))) {
try {
Files.createFile(path);
} catch (IOException e) {
e.printStackTrace();
}
}
try(BufferedWriter buffer = Files.newBufferedWriter(path)){
//для записи в файл
buffer.write(json);
}catch(IOException e) {
System.out.println("Ошибка в записи 1");
e.printStackTrace();
}
return json;
}
}
package com.jsonEx.il;
public class ServiceJson {
private final static String PATH = "src/main/java/save.txt";
private static String json;
public static void main(String[] args) {
String urlAdress = "https://nominatim.openstreetmap.org/search?state=Samara&country=russia&format=json&polygon_geojson=1";
String urlAdress2 = "https://shortiki.com/";
System.out.println("Запуск, содержимоме:");
UrlConnection url = new UrlConnection(urlAdress, PATH);
//url.getUrl();
json = url.getJsonString();
url.saveInFiles(json);
System.out.println(json);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question