I have tool code use Google search API.
My code:
import com.google.gson.Gson;
class GoogleResults {
private ResponseData responseData;
public ResponseData getResponseData() {
return responseData;
}
public void setResponseData(ResponseData responseData) {
this.responseData = responseData;
}
public String toString() {
return "ResponseData[" + responseData + "]";
}
static class ResponseData {
private List<Result> results;
public List<Result> getResults() {
return results;
}
public void setResults(List<Result> results) {
this.results = results;
}
public String toString() {
return "Results[" + results + "]";
}
}
static class Result {
private String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String toString() {
return "Result[url:" + url + " ]";
}
}
}
public class CrawData {
public static void main(String[] args) throws IOException, InterruptedException {
String query;
int n;
int k=0;
String site;
String resultset;
Scanner st = new Scanner(System.in);
System.out.print(" Input key search: ");
query = st.nextLine();
System.out.print("Input site: ");
site = st.nextLine();
System.out.print("Input number of result: ");
n = st.nextInt();
resultset = query + " site:" + site;
for (int j = 0; j < n; j = j + 1) {
Thread.sleep(4000);
String address = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&start="+j+"&q=";
String charset = "UTF-8";
URL url = new URL(address + URLEncoder.encode(resultset, charset));
Reader reader = new InputStreamReader(url.openStream(), charset);
GoogleResults results = new Gson().fromJson(reader,
GoogleResults.class);
int total = results.getResponseData().getResults().size();
// Show title and URL of each results
for (int i = 0; i <= total - 1; i++) {
String Url = results.getResponseData().getResults().get(i)
.getUrl();
k = k+1;
System.out.println("URL: " +Url+ " " + k);
}
}
}
}
when i run it, i have trouble about result return of code.
My system return list url of website.. but it not stable.
Some picture:
my error
Have error: Exception in thread "main" java.lang.NullPointerException
at CrawData.main(CrawData.java:107)
help me...
Sorry my english is too bad.. :(
My guess is that on this line:
String Url = results.getResponseData().getResults().get(i)
.getUrl();
Either get(i) is returning null or getUrl() is returning null. You should add some error handling logic:
if (results.getResponseData().getResults().get(i) != null &&
results.getResponseData().getResults().get(i).getUrl() !=null) {
String Url = results.getResponseData().getResults().get(i)
.getUrl();
k = k+1;
System.out.println("URL: " +Url+ " " + k);
} else {
// Print some type of error here. Try to figure out why the result or the
// url is null
}
Related
I am using the JSON array to get the values from Db in Servlet. I am suing following code
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
List<ProjectDto> act=new ArrayList<ProjectDto>();
act=ActivitiesDao.getActivitiesDetails();
JSONArray jsonarray= new JSONArray();
for(int i=0;i<act.size();i++)
{
JSONObject obj = new JSONObject();
//obj.put("issueno", quiz.get(i).getIssueno());
obj.put("projectName",act.get(i).getProjectName());
obj.put("projectDescription",act.get(i).getProjectDescription());
obj.put("currentStatus", act.get(i).getCurrentStatus());
obj.put("area", act.get(i).getArea());
jsonarray.put(obj);
}
System.out.println("Json in Servelt"+ jsonarray.toString());
response.getWriter().print(jsonarray.toString());
}
}
I am able to get value from DB successfully. I have printed value in console.
Now, I have defined one method in some other java file to convert the data in paragraphs. Now, I am calling that method in Servlet in following way:
//method:
public static List<String> convertParagraphs(String text){
List <String> convertedList= new ArrayList<String>();
if(text==null|| text==""){
}
else{
String[] paragraphs= text.split("\\|");
convertedList= Arrays.asList(paragraphs);
}
return convertedList;
}
calling this method in Servlet:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
List<ProjectDto> act=new ArrayList<ProjectDto>();
act=ActivitiesDao.getActivitiesDetails();
JSONArray jsonarray= new JSONArray();
for(int i=0;i<act.size();i++)
{
JSONObject obj = new JSONObject();
obj.put("projectName",act.get(i).getProjectName());
String ttextt= act.get(i).getProjectDescription();
obj.put("ProjectDescriptionList" , act.get(i).setProjectDescriptionList(NewsletterUtil.convertParagraphs(ttextt)));
obj.put("currentStatus", act.get(i).getCurrentStatus());
obj.put("area", act.get(i).getArea());
obj.put("ProjectDescriptionList" , act.get(i).setProjectDescriptionList(NewsletterUtil.convertParagraphs(ttextt)));
jsonarray.put(obj);
}
System.out.println("Json in Servelt"+ jsonarray.toString());
response.getWriter().print(jsonarray.toString());
}
}
Getters and Setters method:
public class ProjectDto {
private String projectName;
private String projectDescription;
private String currentStatus;
private String area;
private List<String> projectDescriptionList= new ArrayList<String>();
private List<String> currentStatusList=new ArrayList<String>();
public List<String> getCurrentStatusList() {
return currentStatusList;
}
public void setCurrentStatusList(List<String> currentStatusList) {
this.currentStatusList = currentStatusList;
}
public List<String> getProjectDescriptionList() {
return projectDescriptionList;
}
public boolean setProjectDescriptionList(List<String> projectDescriptionList) {
this.projectDescriptionList = projectDescriptionList;
return false;
}
public String getCurrentStatus() {
return currentStatus;
}
public void setCurrentStatus(String currentStatus) {
this.currentStatus = currentStatus;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public String getProjectDescription() {
return projectDescription;
}
public void setProjectDescription(String projectDescription) {
this.projectDescription = projectDescription;
}
#Override
public String toString() {
return "ProjectDto [projectName=" + projectName
+ ", projectDescription=" + projectDescription
+ ", currentStatus=" + currentStatus + ", area=" + area
+ ", projectDescriptionList="
+ projectDescriptionList + "]";
}
}
Issue: Now, I am printing the value of ProjectDescription List, it is returning the false value. I am not sure where i am doing wrong. I tried to resolve the issues in many ways but not able to resolve the issue. could you please correct me and suggest me about my issue.
Please find my ajax code in jsp
<script>
$.ajax({
type: "POST",
url: "./ProjectServlet",
success: function (responseText) {
var jsonData = JSON.parse(responseText);
var uniueTabs = getUniqueLists(jsonData);
for (var i = 0; i < uniueTabs.length; i++) {
$('#tabs').append('<li >' + uniueTabs[i].area + '</li>');
var div = '<div id="tab-content-' + i + '" class="tab-pane fade">';
for (var j = 0; j < uniueTabs[i].tabContent.length; j++) {
var obj = uniueTabs[i].tabContent[j];
div += '<p>' + obj.projectName + '</p>'+ '<p>' + obj.projectDescription + '</p>' + obj.currentStatus ;
}
$('.tab-content').append(div);
$('#tab-content-0').addClass('in active');
}
$('#tabs li').eq(0).addClass('active');
setListner();
function getUniqueLists(responseText) {
var resArr = [];
responseText.filter(function (x, i) {
if (resArr.indexOf(x.area) === -1) {
resArr.push(x.area);
}
})
//console.log(resArr);
return mergeDataAreaWise(resArr, responseText);
}
function mergeDataAreaWise(area, responseText) {
var tabList = [];
for (var i = 0; i < area.length; i++) {
tabList.push({
area: area[i],
tabContent: []
});
}
var prjlist;
var output;
var proh;
var status;
for (var i = 0; i < tabList.length; i++) {
for (var j = 0; j < responseText.length; j++) {
statuss = '<li>' + responseText[j].currentStatus + '</li>';
prjlist= responseText[j].projectDescriptionList;
/* output= prjlist.split("&");
proh=output.join("/n");
*/
//proh=prjlist.replace(/&/g, '<br>');
status =statuss.replace(/&/g, '<br>');
var Obj = {
projectName: '<h3>'+ responseText[j].projectName +'</h3>',
projectDescription: '<p>'+ prjlist + '<p>',
currentStatus: '<b>'+ "Current Status:" +'</b>' + status
}
var currentArea = responseText[j].area;
if (tabList[i].area === currentArea) {
tabList[i].tabContent.push(Obj);
}
}
}
console.log(tabList);
return tabList;
}
}
});
function setListner () {
$("#tabs a").click(function () {
$(this).tab('show');
});
}
</script>
Of course you will get the value as false.
in your setter you are returning a boolean, which does set the value of projectDescriptionList but finally returns the hard code false every time.
Solution,
In your getter-setter;
// optional, just stick to setter rules, set the value and don't return anything.
public setProjectDescriptionList(List<String> projectDescriptionList) {
this.projectDescriptionList = projectDescriptionList;
}
In your doPost inside the for loop, third part of code in question
act.get(i).setProjectDescriptionList(NewsletterUtil.convertParagraphs(ttextt));
obj.put("ProjectDescriptionList" ,act.get(i).getProjectDescriptionList() );
obj.put("currentStatus", act.get(i).getCurrentStatus());
obj.put("area", act.get(i).getArea());
// I don't know why you wrote it twice.. I'm commenting it.
// obj.put("ProjectDescriptionList" , act.get(i).setProjectDescriptionList(NewsletterUtil.convertParagraphs(ttextt)));
jsonarray.put(obj);
I am working on a personal Java project and learning how to print out data from a text file. My code (as you can see below) prints out the userNameGenerator and personName data perfectly fine but I want it to be printed out from the toString in my Java Class. How can I change my code to print it out from there?
This is how my toString looks like:
#Override
public String toString() {
return userNameGenerator + " -> " + "[" + personName + "]" ;
}
Full code:
import java.util.*;
import java.io.*;
public class Codes {
public static void main(String[] args) {
List<Codes2> personFile = new ArrayList<>();
try {
BufferedReader br = new BufferedReader(new FileReader("person-data.txt"));
String fileRead = br.readLine();
while (fileRead != null) {
String[] personData = fileRead.split(":");
String personName = personData[0];
String userNameGenerator = personData[1];
Codes2 personObj = new Codes2(personName, userNameGenerator);
personFile.add(personObj);
fileRead = br.readLine();
}
br.close();
}
catch (FileNotFoundException ex) {
System.out.println("File not found!");
}
catch (IOException ex) {
System.out.println("An error has occured: " + ex.getMessage());
}
Set<String> newStrSet = new HashSet<>();
for(int i = 0; i < personFile.size(); i++){
String[] regionSplit = personFile.get(i).getUserNameGenerator().split(", ");
for(int j = 0; j < regionSplit.length; j++){
newStrSet.add(regionSplit[j]);
}
}
for (String p: newStrSet) {
System.out.printf("%s -> ", p);
for (Codes2 s: personFile) {
if (s.getUserNameGenerator().contains(p)) {
System.out.printf("%s, ", s.getPersonName());
}
}
System.out.println();
}
}
}
Java Class:
public class Codes2 implements Comparable<Codes2> {
private String personName;
private String userNameGenerator;
public Codes2(String personName, String userNameGenerator) {
this.personName = personName;
this.userNameGenerator = userNameGenerator;
}
public String getPersonName() {
return personName;
}
public String getUserNameGenerator() {
return userNameGenerator;
}
#Override
public int compareTo(Codes2 o) {
return getUserNameGenerator().compareTo(o.getUserNameGenerator());
}
public int compare(Object lOCR1, Object lOCR2) {
return ((Codes2)lOCR1).userNameGenerator
.compareTo(((Codes2)lOCR2).userNameGenerator);
}
#Override
public String toString() {
return userNameGenerator + " -> " + "[" + personName + "]" ;
}
}
Everything looks right in your code.
I think you should just call the method when you are trying to print it out:
for (Codes2 s: personFile) {
if (s.getUserNameGenerator().contains(p)) {
System.out.printf("%s, ", s.toString());
}
}
This follows the fact that the class that prints out the object is not so closely coupled with the class that hold the data.
Try:
#Override
public String toString() {
return String.format("%s -> [%s]",this.userNameGenerator,this.personName);
}
I am trying to run my code and I keep getting error. The json file I'm reading in is completely fine with the keys and values. And the one I am trying to write out is just a json file with "{ }" and that's it. Please help me. It's the only error I'm getting.
MAIN CLASS
import java.io.Serializable;
class LibraryOfMovieDescriptions implements Serializable {
public static void main(String[] args){
MovieLibrary movieLibrary;
movieLibrary = new MovieLibrary("movies.json");
movieLibrary.toJsonFile("movies2.json");
}
}
MovieDescription Class
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;
import java.io.Serializable;
public class MovieDescription implements Serializable {
private String title;
private String rating;
private String release;
private String runtime;
private String plot;
private String filename;
private String genre;
private String actors;
public JSONObject toJSONObject() throws JSONException {
JSONObject obj = new JSONObject();
obj.put("Title", title);
obj.put("Rated", rating);
obj.put("Released", release);
obj.put("Runtime", runtime);
obj.put("Plot", plot);
obj.put("Filename", filename);
JSONArray a = new JSONArray();
String[] sArray = this.actors.split(" , ");
for(int i = 0; i < sArray.length; i++){
a.put(i);
}
obj.put("Actors", a);
JSONArray g = new JSONArray();
String[] gArray = this.genre.split(" , ");
for(int i = 0; i < gArray.length; i++){
g.put(i);
}
obj.put("Genre", g);
return obj;
}
public MovieDescription(JSONObject jsonObj) throws JSONException{
this.title = jsonObj.getString("Title");
this.rating = jsonObj.getString("Rated");
this.release = jsonObj.getString("Released");
this.plot = jsonObj.getString("Plot");
this.runtime = jsonObj.getString("Runtime");
this.filename = jsonObj.getString("Filename");
JSONArray g = jsonObj.getJSONArray("Genre");
for(int i = 0; i < g.length(); i++){
this.genre += g.get(i) + ", ";
}
JSONArray a = jsonObj.getJSONArray("Actors");
for(int i = 0; i < a.length(); i++){
this.actors += a.get(i) + ", ";
}
}
public MovieDescription(){
title = " ";
rating = " ";
release = " ";
runtime = " ";
plot = " ";
filename = " ";
genre = " ";
actors = " ";
}
public MovieDescription(String title, String rating, String release, String runtime, String plot, String filename,
String genre, String actors){
this.title = title;
this.rating = rating;
this.release = release;
this.runtime = runtime;
this.plot = plot;
this.filename = filename;
this.genre = genre;
this.actors = actors;
}
public void setTitle(String title){
this.title = title;
}
public String getTitle(){
return title;
}
public void setRating(String rating){
this.rating = rating;
}
public String getRating(){
return rating;
}
public void setRelease(String release){
this.release = release;
}
public String getRelease(){
return this.release;
}
public void setRuntime(String runtime){
this.runtime = runtime;
}
public String getRuntime(){
return runtime;
}
public void setPlot(String plot){
this.plot = plot;
}
public String getPlot(){
return plot;
}
public void setFilename(String filename){
this.filename = filename;
}
public String getFilename(){
return filename;
}
public void setGenre(String genre){
this.genre = genre;
}
public String getGenre(){
return genre;
}
public void setActors(String actors){
this.actors = actors;
}
public String getActors(){
return actors;
}
public String toString(){
String string = ("Title: " + title + "\n" + "Rating: " + rating + "\n" + "Released: " + release + "\n" +
"Runtime: " + runtime + "\n" + "Plot: " + plot + "\n" + "Filename: " + filename + "\n" + "Genre: " + genre
+ "\n" + "Actors: " + actors + "\n");
return string;
}
}
MovieLibrary Class
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.json.JSONTokener;
import java.io.Serializable;
import java.io.ObjectOutputStream;
public class MovieLibrary implements Serializable {
private List<MovieDescription> movieLib = new ArrayList<MovieDescription>();
private int arraySize;
public MovieLibrary(){
arraySize = 0;
}
public boolean isEmpty(){
return arraySize == 0;
}
public MovieDescription get(String aTitle){
int i = indexOf(aTitle);
if(i == -1){
return null;
}
return movieLib.get(i);
}
public boolean add(MovieDescription aClip){
movieLib.add(aClip);
arraySize++;
return true;
}
public boolean remove(String aTitle){
int i = indexOf(aTitle);
if(i != -1){
movieLib.remove(i);
arraySize--;
}
return true;
}
public String[] getTitles(){
String[] s = new String[movieLib.size()];
for(int i = 0; i < movieLib.size(); i++){
s[i] = movieLib.get(i).getTitle();
}
return s;
}
private int indexOf(String aTitle){
for(int i = 0; i < movieLib.size(); i++)
if(((movieLib.get(i)).getTitle()).equals(aTitle)){
return i;
}
return -1;
}
public MovieLibrary(String jsonFile){
FileInputStream in = null;
JSONObject jsonObj = null;
String[] titles;
try{
in = new FileInputStream(jsonFile);
jsonObj = new JSONObject(new JSONTokener(in));
titles = JSONObject.getNames(jsonObj);
System.out.println("Adding Movies...");
for(int i = 0; i < titles.length; i++){
JSONObject temp = jsonObj.getJSONObject(titles[i]);
MovieDescription movies = new MovieDescription(temp);
movieLib.add(movies);
}
System.out.println(this.getTitles());
in.close();
}
catch(Exception e){
e.printStackTrace();
if(in != null){
try{
in.close();
}
catch(IOException z){
z.printStackTrace();
System.exit(0);;
}
}
}
}
public void toJsonFile(String jsonFileName){
JSONObject jsonObj = new JSONObject();
ObjectOutputStream out = null;
try{
for(int i = 0; i < movieLib.size(); i++)
jsonObj.put(movieLib.get(i).getTitle(),movieLib.get(i).toJSONObject());
out = new ObjectOutputStream(new FileOutputStream(jsonFileName));
out.writeObject(jsonObj.toString());
out.close();
}
catch(Exception e){
e.printStackTrace();
if(out != null){
try{
out.close();
}
catch(IOException z){
z.printStackTrace();
System.exit(0);
}
}
}
}
}
And here is the error I am getting:
Adding Movies...
[Ljava.lang.String;#4554617c
Also, after I run the program and check my "movies2.json" (the one I'm trying to write to) the file turns to this.
weird stuff:
The first message is because System.out.println is passed an array of strings (this.getTitles()). To display this array of string, one must convert it to a single string Arrays.toString(this.getTitles()), or use a for loop iterating on the array to display one title per line.
The wrong output is because ObjectOutputStream is an output stream for serializing Java objects (even though in this case it is a String object) in binary. FileWriter should be a better option to consider, as its API allows you to write characters to a file.
I would just have the id, the content and the hashtag and time of every tweet in a text file, and I dont know how to store infomration in lists of tweet,
I created a tweet class as follows:
public class Tweet {
private String type;
private String origin;
private String tweetText;
private String url;
private String tweetID;
private String tweetDate;
private int retCount;
private String favourit;
private String mEntities;
private String hashtags;
public Tweet(String tweetID,String origin) {
this.tweetID = tweetID;
this.origin = origin;
}
public Tweet(String type, String origin, String tweetText, String url, String tweetID, String tweetDate, int retCount, String favourit, String mEntities, String hashtags) {
this.type = type;
this.origin = origin;
this.tweetText = tweetText;
this.url = url;
this.tweetID = tweetID;
this.tweetDate = tweetDate;
this.retCount = retCount;
this.favourit = favourit;
this.mEntities = mEntities;
this.hashtags = hashtags;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getOrigin() {
return origin;
}
public void setOrigin(String origin) {
this.origin = origin;
}
public String getTweetText() {
return tweetText;
}
public void setTweetText(String tweetText) {
this.tweetText = tweetText;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTweetID() {
return tweetID;
}
public void setTweetID(String tweetID) {
this.tweetID = tweetID;
}
public String getTweetDate() {
return tweetDate;
}
public void setTweetDate(String tweetDate) {
this.tweetDate = tweetDate;
}
public int getRetCount() {
return retCount;
}
public void setRetCount(int retCount) {
this.retCount = retCount;
}
public String getFavourit() {
return favourit;
}
public void setFavourit(String favourit) {
this.favourit = favourit;
}
public String getmEntities() {
return mEntities;
}
public void setmEntities(String mEntities) {
this.mEntities = mEntities;
}
public String getHashtags() {
return hashtags;
}
public void setHashtags(String hashtags) {
this.hashtags = hashtags;
}
and my data file has the following format:
***
***
Type:status
Origin: Here's link to listen live to our discussion of #debtceiling #politics :
Text: Here's link to listen live to our discussion of :
URL:
ID: 96944336150867968
Time: Fri Jul 29 09:05:05 CDT 2011
RetCount: 0
Favorite: false
MentionedEntities:
Hashtags: debtceiling politics
***
***
Type:status
Origin: Now we're talking #debtceiling w/ Dick Polman #NewsWorksWHYY #PhillyInquirer & Bill Galston #BrookingsInst #NoLabelsOrg
Text: Now we're talking w/ Dick Polman & Bill Galston
URL:
ID: 96943803600089088
Time: Fri Jul 29 09:02:58 CDT 2011
RetCount: 1
Favorite: false
MentionedEntities: 136337303 151106990 14495726 15161791
Hashtags: debtceiling
***
***
I want to read this file and stock information into lists, i begin with this code, but i dont know how to solve that
public static List<String> readTweets(File file) throws IOException {
List<String> tweets = new ArrayList<String>();
//logger.info("Read tweets from {}", file.getAbsolutePath());
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
String[] fields;
while ((line = reader.readLine()) != null) {
fields = line.split(",");
if (fields.length > 1)
tweets.add(fields[1]);
}
return tweets;
}
By the looks of the code you are experimenting with, this is what I would do:
public static List<String> readTweets(File file) throws IOException {
List<String> tweets = new ArrayList<String>();
List<String> lines = Files.readAllLines(file.toPath());
for(int i = 0; i < lines.length(); i++){
String line = lines.get(i);
String[] part = line.split(",");
if(part.length < 1) tweets.add(part[i]);
}
}
But, if I wrote an application that was purely for printing the contents of tweets into the console, here's how I'd do it:
TweetReader.java
package Testers;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
public class TweetReader {
public static List<Tweet> readTweets(File file) throws IOException {
boolean processEnd = false;
String type = "";
String origin = "";
String tweetText = "";
String url = "";
String tweetID = "";
String tweetDate = "";
int retCount = 0;
String favourite = "";
String mEntities = "";
String hashTags = "";
List<Tweet> tweets = new ArrayList<Tweet>();
List<String> lines = Files.readAllLines(file.toPath());
for(int i = 0; i < lines.size(); i++){
String line = lines.get(i);
line = line.trim();
if(line.equals("***")){
if(processEnd){
Tweet tweet = new Tweet(type, origin, tweetText, url, tweetID, tweetDate, retCount, favourite, mEntities, hashTags);
tweets.add(tweet);
processEnd = false;
}else processEnd = true;
}else{
if(line.contains(":")){
String header = line.substring(0, line.indexOf(":"));
//System.out.println(header); //You can uncomment this for troubleshooting
if(header.equals("Type")) type = line.substring(line.length() > 5 ? 5 : line.length());
else if(header.equals("Origin")) origin = line.substring(line.length() > 8 ? 8 : line.length());
else if(header.equals("Text")) tweetText = line.substring(line.length() > 6 ? 6 : line.length());
else if(header.equals("URL")) url = line.substring(line.length() > 5 ? 5 : line.length());
else if(header.equals("ID")) tweetID = line.substring(line.length() > 4 ? 4 : line.length());
else if(header.equals("Time")) tweetDate = line.substring(line.length() > 6 ? 6 : line.length());
else if(header.equals("RetCount")) retCount = Integer.parseInt(line.substring(line.length() > 10 ? 10 : line.length()));
else if(header.equals("Favorite")) favourite = line.substring(line.length() > 11 ? 11 : line.length());
else if(header.equals("MentionedEntities")) mEntities = line.substring(line.length() > 19 ? 19 : line.length());
else if(header.equals("Hashtags")) hashTags = line.substring(line.length() > 10 ? 10 : line.length());
else throw new IOException("Line cannot be identified as part of a tweet:" + line);
}else throw new IOException("Line cannot be processed:" + line);
}
}
return tweets;
}
public static void main(String[] args){
File log = new File("log.txt");
List<Tweet> tweets = new ArrayList<Tweet>();
try {
File f = new File(".").getAbsoluteFile();
File[] array = f.listFiles();
for(int i = 0; i < array.length; i++){
File tweet = array[i];
if(tweet.isFile() && !tweet.getName().contains("log.txt") && !tweet.getName().contains(".jar")){
log("Reading file: " + tweet.getAbsolutePath(), log);
List<Tweet> tweetlist = readTweets(tweet);
tweets.addAll(tweetlist);
}
}
System.out.println("Reading tweets now");
for(int i = 0; i < tweets.size(); i++){
Tweet t = tweets.get(i);
log("Type = " + t.getType(), log);
log("Origin = " + t.getOrigin(), log);
log("Text = " + t.getTweetText(), log);
log("URL = " + t.getURL(), log);
log("ID = " + t.getTweetID(), log);
log("Date = " + t.getTweetDate(), log);
log("Ret count = " + t.getRetCount(), log);
log("Favourite = " + t.getFavourite(), log);
log("Mentioned entities = " + t.getMentionedEntities(), log);
log("Hashtags = " + t.getHashtags(), log);
log("Tweet finished", log);
}
} catch (IOException e) {
log(e, log);
}
log("Finished reading tweets.", log);
}
private static void log(IOException e, File log) {
log(e.getMessage(), log);
StackTraceElement[] array = e.getStackTrace();
for(int i = 0; i < array.length; i++){
log(" " + array[i], log);
}
}
private static void log(String string, File log) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(log, true));
writer.write(string);
writer.newLine();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Tweet.java
package Testers;
public class Tweet {
private String type;
private String origin;
private String tweetText;
private String url;
private String tweetID;
private String tweetDate;
private int retCount;
private String favourit;
private String mEntities;
private String hashtags;
public Tweet(String tweetID,String origin) {
this.tweetID = tweetID;
this.origin = origin;
}
public Tweet(String type, String origin, String tweetText, String url, String tweetID, String tweetDate, int retCount, String favourit, String mEntities, String hashtags) {
this.type = type;
this.origin = origin;
this.tweetText = tweetText;
this.url = url;
this.tweetID = tweetID;
this.tweetDate = tweetDate;
this.retCount = retCount;
this.favourit = favourit;
this.mEntities = mEntities;
this.hashtags = hashtags;
}
public String getType() {
return type;
}
public String getOrigin(){
return origin;
}
public String getTweetText(){
return tweetText;
}
public String getURL(){
return url;
}
public String getTweetID(){
return tweetID;
}
public String getTweetDate(){
return tweetDate;
}
public int getRetCount(){
return retCount;
}
public String getFavourite(){
return favourit;
}
public String getMentionedEntities(){
return mEntities;
}
public String getHashtags(){
return hashtags;
}
}
//global attribute
List<Tweet> tweetList = new ArrayList<>();
String line = "";
String[] fields;
while (line != null) {
line = reader.readLine();
line = reader.readLine();
//these two are for ***
for(int i = 0;i<10;i++){
line = reader.readLine();
tweets.add(line);
// these are for the other data
}
Tweet tweet = createTweetFromList(tweets);
tweetList.add(tweet);
}
I am trying to implement paging in an already running application, it works like mongodb where you get a query with bunch of parameters like
searchConditions ,limit( how many docs you want), skip ( skip these many docs)
and so on
and the response is back in json my task is to generate the previous and next link to the query that means previous page and the next page as per query
I came up with a method below
public Paging generateLink(RequestFilter filter) {
int prev_skip;
int prev_limit;
String pagePrev = "";
String pageNext = "";
int next_skip;
int next_limit;
String searchCondition = JsonUtils.toSimpleJsonString(filter.getSearch());
String url = "http://isgswmdi1n1.nam.nsroot.net:7014/account-search/rest/api/v1/pmm";
//properties.getProperty("paging.link");
System.out.println(url);
Paging pagingOption = new Paging();
Result result = new Result();
int count = luceneSearchService.searchCount(filter);
try {
if (count > 1) {
if (filter.getSkip() > 0) {
prev_skip = Math.max((filter.getSkip() - 1), 0);
prev_limit = filter.getLimit();
pagePrev = url + "?search=" + searchCondition + "&skip=" + prev_skip + "$limit=" + prev_limit;
} else{
result.setPaging(null);
return null;
}
if (count > (filter.getLimit() + filter.getSkip())) {
next_skip = (filter.getSkip() + filter.getLimit());
next_limit = filter.getLimit();
pageNext = url + "?search=" + searchCondition + "&skip=" + next_skip + "$limit=" + next_limit;
} else{
result.setPaging(null);
return null;
}
pagingOption.setPagePrev(pagePrev);
pagingOption.setPageNext(pageNext);
result.setPaging(pagingOption);
}
return pagingOption;
} catch (NullPointerException n)
{
n.printStackTrace();
return pagingOption;
}
}
call to generateLink method
return Result.ok(resultRow, generateLink(filter));
the ok response method in the Result class
public static Result ok(List<ResultRow> resultRows, Paging paging) {
if (paging != null) {
return new Result(resultRows, 200, "Completed", paging);
} else
return new Result(resultRows, 200, "Completed");
}
Underlying paging class
public class Paging implements Serializable {
public String getPagePrev() {
return pagePrev;
}
public void setPagePrev(String pagePrev) {
this.pagePrev = pagePrev;
}
#JsonProperty("pagePrev")
private String pagePrev;
public String getPageNext() {
return pageNext;
}
public void setPageNext(String pageNext) {
this.pageNext = pageNext;
}
#JsonProperty("pageNext")
private String pageNext;
}
but my test method is failing
#Test
public void search_allFilterParamsAreDefined_hp() throws Exception {
// given
Map<String, Serializable> searchConditions = singletonMap("shortName", "GO");
List<String> returnFields = asList("shortname", "gfcid");
String returnFieldsStr = returnFields.stream().collect(joining(","));
RequestFilter filter = RequestFilter.create(searchConditions, returnFieldsStr, 5, 10, true);
int resultSize = 20;
List<ResultRow> searchResult = getSearchResult(resultSize);
when(luceneSearchService.search(filter)).thenReturn(searchResult);
when(luceneSearchService.searchCount(filter)).thenReturn(resultSize);
// do
Result result = searchCoordinator.search(filter);
// verify
assertEquals(searchResult, result.getRows());
assertReturnFields(returnFields, result.getRows());
assertNotNull(result.getPaging());
Map<String, String> nextParams = getQueryParams(result.getPaging().getPageNext());
assertParam(nextParams, "search", toSimpleJsonString(searchConditions));
assertParam(nextParams, "skip", "15");
assertParam(nextParams, "limit", "10");
}