Request get in java for get source html - java

I have this request in Java but in the response i haven't all source, and Json object fail..
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(
"http://url?"+params);
HttpResponse response = client.execute(request);
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
String str = result.toString();
Document doc = Jsoup.parse(str);
JSONObject json = new JSONObject();
List<JSONObject> list = new ArrayList<JSONObject>();
Element elementsTable = doc.getElementById("shortlist");
this element shortlist can't find that..but if i go with my browser and get the source there is..where i'm wrong?

You can do it with Jsoup with simpler code ...
Connection connection = Jsoup.connect(url)
Document urlDoc = connection.get();
Elements domElement = urlDoc.getElementById(String id);

Related

How to support multi language in GSON (between Json and Java Object)?

In my project , i separated back-end and front-end modules and run by providing REST api from back-end and call it by using Apache Http Client and GSON.
I want to provide multiple language like German,French... on UI(webpage).
On webpage , It shows like this "Schl��ssli Sch��negg, Wilhelmsh��he" , but in database and RestAPI json is "Schlössli Schönegg" .
How can I support multi language?
In back-end , i wrote Request methods like get,put,post and In Front-end, i used HttpClient and GSON to convert JSON to/from Java Object.
I tried inside the html but main problem is from GSON when it convert fromJSON() , the original JSON value ""Schlössli Schönegg" become "Schl��ssli Sch��negg, Wilhelmsh��he".
In RestAPI , JSON data is
{
"addressId": 3,
"buildingName": "Schlössli Schönegg",
"street": "Wilhelmshöhe",
"ward": "6003",
"district": "luzern",
"cityOrProvince": "luzern state",
"country": "Switzerland"
}
But in Front-end , Java Object String Data after GSON convert is
(..buildingName=Schlössli Schönegg, street=Wilhelmshöhe, ward=6003, district=luzern, cityOrProvince=luzern state, country=Switzerland)
Here , RestClient function code
public List<FakeEmployeeDTO> getAllEmployeeList() throws IOException {
HttpClient client = HttpClientBuilder.create().build();
HttpGet getRequest = new HttpGet(URL);
HttpResponse response = client.execute(getRequest);
Integer statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
throw new SystemException(ERROR_MESSAGE + statusCode);
}
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
StringBuilder jsonData = new StringBuilder();
while ((line = rd.readLine()) != null) {
jsonData.append(line);
}
response.getEntity().getContent().close();
rd.close();
logger.info(jsonData.toString());
Gson gson = new GsonBuilder().setDateFormat("dd-MMM-yyyy").create();
Type listType = new TypeToken<List<FakeEmployeeDTO>>() {
}.getType();
List<FakeEmployeeDTO> employeeList = gson.fromJson(jsonData.toString(), listType);
sortEmployeeListByFirstName(employeeList);
return employeeList;
}
Inside Employee, I have address atrribute , inside that address i have value like buildingNumber and Street, that value can be in any languages.
Try with this
(BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent(),
"UTF-8"));)
Entire Code will end up with like this.
public List<FakeEmployeeDTO> getAllEmployeeList() throws IOException {
HttpClient client = HttpClientBuilder.create().build();
HttpGet getRequest = new HttpGet(url);
HttpResponse response = client.execute(getRequest);
Integer statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
throw new SystemException(ERROR_MESSAGE + statusCode);
}
BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent(),
"UTF-8")); // if it is not working please try with ("ISO-8859-1")
String line = "";
StringBuilder jsonData = new StringBuilder();
while ((line = rd.readLine()) != null) {
jsonData.append(line);
}
response.getEntity().getContent().close();
rd.close();
logger.info(jsonData.toString());
Gson gson = new GsonBuilder().setDateFormat("dd-MMM-yyyy").create();
Type listType = new TypeToken<List<FakeEmployeeDTO>>() {
}.getType();
List<FakeEmployeeDTO> employeeList = gson.fromJson(jsonData.toString(), listType);
sortEmployeeListByFirstName(employeeList);
return employeeList;}
Next time make sure to try with this, because you can use "try with resource"
try (BufferedReader reader = new BufferedReader(new InputStreamReader(
new URL("https://htt.your url.com" + URLEncoder.encode(query, "UTF-8") )
.openConnection().getInputStream()))) {

Json System.err: Unexpected character () at position 0. only from php

URL url = new URL(host);
urlConnection = (HttpURLConnection) url.openConnection();
int code = urlConnection.getResponseCode();
System.out.print(code);
if (code==200) {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
if (in != null) {
String content = in.toString();
System.out.print(content);
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(new InputStreamReader(in, "UTF-8"));
result = (String) jsonObject.get("name");
System.out.print(jsonObject);
}
in.close();
}
When I have a host string like http://www.example.com/json.txt it all works fine, but when I have host string like www.example.com/index.php?data=data&data2=data2 I get the following error:
W/System.err: Unexpected character () at position 0.
I/System.out: 200java.io.BufferedInputStream#8bc189fpp = [0, 700, 250, 700]
My PHP output in browser looks fine, when I copy it to json.txt it also works fine.
I try to play with urlConnection POST, GET, RAW without luck.
Any ideas?
Problem was probly BOM in php json.
I do:
URL url = new URL(host);
urlConnection = (HttpURLConnection) url.openConnection();
int code = urlConnection.getResponseCode();
System.out.print(code);
if(code==200){
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
StringWriter writer = new StringWriter();
if (in != null) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = bufferedReader.readLine()) != null)
result += line;
result = result.substring(1);
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject)jsonParser.parse(result);
result=(String) jsonObject.get("name");
System.out.print(jsonObject);
}
in.close();
}
And it works.

Text file input using buffer reader, parse it and store in json array format using java and make one PUT api call

I have a text file where i am trying to fetch data one by one and store it in rri section with current time stamp under one PUT call but currently its fetching only one value or multiple request? Kindly help me to get through it. I will be sharing the text file and expected result in the comment section below.
File file = new File("DataToInput.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String line;
String urlString= "http://34.237.64.52 /v1/iot-hitoe-analytics/groups/0d98a4d3-207c-41e3-a943-363b73d58adf/raw-data";
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.addRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("PUT");
//conn.connect();
conn.setRequestProperty("content-type", "application/json");
conn.setRequestProperty("Accept", "application/json");
JSONObject Main = new JSONObject();
JSONArray alldata = new JSONArray();
JSONObject alldata1 = new JSONObject();
JSONObject metadata = new JSONObject();
metadata.put("sensorTypeId", sensorTypeId);
metadata.put("sensorId", sensorId);
metadata.put("sensorName", sensorName);
metadata.put("connectionMode",connectionMode);
JSONObject owner = new JSONObject();
owner.put("groupId", groupId);
owner.put("entityId", entityId);
JSONObject sensordata = new JSONObject();
JSONObject rawRri = new JSONObject();
JSONArray samples_rri = new JSONArray();
JSONObject samples1_rri = new JSONObject();
while ((line = bufferedReader.readLine()) != null) {
String[] fields = line.split(" ");
// System.out.println("fields size is "+fields.length);
// System.out.println("fields value is "+fields[0]);
stringBuffer.append(line);
stringBuffer.append("\n");
}
samples1_rri.put("rri",line);
samples1_rri.put("timestamp",System.currentTimeMillis());
samples_rri.put(samples1_rri);
rawRri.put("samples", samples_rri);
sensordata.put("rawRri", rawRri);
alldata1.put("metadata", metadata);
alldata1.put("owner", owner);
alldata1.put("sensordata", sensordata);
alldata.put(alldata1);
Main.put("alldata", alldata);
String payload = Main.toString();
ObjectMapper mapper = new ObjectMapper();
#SuppressWarnings("unused")
Object json = mapper.readValue(payload, Object.class);
String indented = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
System.out.println(indented);
/*OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
writer.write(indented);
writer.close();*/
fileReader.close();
bufferedReader.close();
//System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
URLEncoder.encode(urlString,"UTF-8");
System.out.println("response codec: "+conn.getResponseCode());
// if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
// throw new RuntimeException("Failed : HTTP error code : "
// + conn.getResponseCode());
System.out.println("response received!!");
}
else {
System.out.println("response: "+conn.getInputStream());
}
BufferedReader br1 = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br1.readLine()) != null) {
System.out.println(output);
}

Internet request works the first time, but not on subsequent requests

On the first request it goes to the internet and retrieves the data. When I try it again, it just gives me the data that is already in the inputstream. How do I clear the cached data, so that it does a new request each time?
This is my code:
InputStreamReader in = null;
in = new InputStreamReader(url.openStream());
response = readFully(in);
url.openStream().close();
Recreate the URL object each time.
You can create a URLConnection and explicitly set the caching policy:
final URLConnection conn = (URLConnection)url.openConnection();
conn.setUseCaches(false); // Don't use a cached copy.
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
See The Android URLConnection documentation for more details.
HttpClient API might be useful
address = "http://google.com"
String html = "";
HttpClient httpClient = DefaultHttpClient();
HttpGet httpget = new HttpGet(address);
BufferedReader in = null;
HttpResponse response = null;
response = httpClient.execute(httpget);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.seperator");
while ((l = in.readLine()) != null) {
sb.append(l + nl);
}
in.close();
html = sb.toString();
You'll need to catch some exceptions

Android Java strange issue building URL

So I'm building an URL to be called to get a JSON response but facing a strange issue. Building the URL as shown below returns "Not found" but for testing purposes I just built the URL as such "http://api.themoviedb.org/3/search/person?api_key=XXX&query=brad" and didn't append anything and that returned the correct response. Also tried not encoding "text" and same thing...Not found. Any ideas?
StringBuilder url = new StringBuilder();
url.append("http://api.themoviedb.org/3/search/person?api_key=XXX&query=").append(URLEncoder.encode(text, ENCODING));
Log.v("URL", url.toString());
try {
HttpGet httpRequest = null;
httpRequest = new HttpGet(url.toString());
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
InputStream input = bufHttpEntity.getContent();
String result = toString(input);
//JSONObject json = new JSONObject(result);
return result;
Try using the code I have below. I've copied and pasted it out of some code I use and I know it works. May not solve your problem but I think its worth a shot. I've edited it a little bit and it should just be copy and paste into your code now.
HttpGet request = new HttpGet(new URI(url.toString()));
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(request);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
StringBuilder builder = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null;) {
builder.append(line).append("\n");
}
JSONObject jResponse = new JSONObject(builder.toString());

Categories

Resources