aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorprithugoswami <prithugoswami524@gmail.com>2019-05-01 19:36:27 +0530
committerprithugoswami <prithugoswami524@gmail.com>2019-05-01 19:36:27 +0530
commitb1a383bc959a9950a0630b1ec953717495199664 (patch)
tree496e6e76c257e43870ea29759799b838ca227200
parentb820fcafd3dc14e660da50e6017c651dab89e38c (diff)
downloadtmdb-desktop-client-b1a383bc959a9950a0630b1ec953717495199664.tar.gz
tmdb-desktop-client-b1a383bc959a9950a0630b1ec953717495199664.zip
Update Search class and Main
Did some reformating and Refactoring Search class will now accept spaces in the query String.
-rw-r--r--src/com/oocpro/tmdbdesktop/Main.java8
-rw-r--r--src/com/oocpro/tmdbdesktop/tmdb/Search.java63
2 files changed, 34 insertions, 37 deletions
diff --git a/src/com/oocpro/tmdbdesktop/Main.java b/src/com/oocpro/tmdbdesktop/Main.java
index bbe2edc..3b31e12 100644
--- a/src/com/oocpro/tmdbdesktop/Main.java
+++ b/src/com/oocpro/tmdbdesktop/Main.java
@@ -2,18 +2,20 @@ package com.oocpro.tmdbdesktop;
import com.oocpro.tmdbdesktop.tmdb.Movie;
import com.oocpro.tmdbdesktop.tmdb.Search;
+
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class Main{
- public static void main (String [] args)throws IOException{
+ public static void main (String[] args)throws IOException{
System.out.println("Enter a movie name to search ");
- BufferedReader mname = new BufferedReader(new InputStreamReader(System.in));
+ BufferedReader mname = (new BufferedReader(new InputStreamReader
+ (System.in)));
String Mov_name = mname.readLine();
Search mv = new Search(Mov_name);
- Movie det = new Movie(Search.IDs.get(1));
+ Movie det = new Movie(mv.movieIds.get(1));
System.out.println(det.title + " ("+det.year+")");
System.out.println("Plotline: " + det.plotline);
System.out.println("Genres: " + det.genres);
diff --git a/src/com/oocpro/tmdbdesktop/tmdb/Search.java b/src/com/oocpro/tmdbdesktop/tmdb/Search.java
index 71bd808..430bddd 100644
--- a/src/com/oocpro/tmdbdesktop/tmdb/Search.java
+++ b/src/com/oocpro/tmdbdesktop/tmdb/Search.java
@@ -1,45 +1,40 @@
package com.oocpro.tmdbdesktop.tmdb;
import com.oocpro.tmdbdesktop.Constants;
-import java.net.URL;
+
+import java.io.InputStream;
+import java.io.IOException;
import java.net.MalformedURLException;
-import java.io.*;
-import org.json.*;
-import javax.swing.*;
+import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
+import org.json.*;
+
+public class Search{
+ final String SEARCH_URL = (Constants.BASEURL + "/search/movie?api_key=" +
+ Constants.API_KEY + "&query=%s");
- public class Search{
- public static List<Integer> IDs;
- public Search(String MOVIE_NAME)throws IOException,MalformedURLException{ //CONSTRUCTOR TO INITIALISE MOVIE ID
- String s_url=Constants.BASEURL+"/search/movie?api_key="+Constants.API_KEY+"&language=en-US&query=%s"; //General URL format for finding a movie
- String url=String.format(s_url,MOVIE_NAME); //Adding the query
- System.out.println("URL = "+url);
- URL us1 = new URL(url); //Creating url object
- InputStream sres = (InputStream)us1.getContent(); //Storing the stream of data we get from the url
- JSONObject ja = new JSONObject(new JSONTokener(sres));
- IDs = new ArrayList<Integer>();
- Iterator IDS_itr = ja.getJSONArray("results").iterator();
- //Iterating through all the results generated by the query and
- //getting the IDs of each result
- while(IDS_itr.hasNext()){
- JSONObject id = (JSONObject)IDS_itr.next();
- IDs.add(id.getInt("id"));
- }
-
- //System.out.println(IDs.get(1) + " ");
- //System.out.println(ja.toString(1));
-
-
- //System.out.println(ja.getNames(ja));
- //System.out.println(" "+ja.getInt("total_pages"));
-
-
+ public List<Integer> movieIds;
+
+ public Search(String query) throws IOException,MalformedURLException {
+
+ String url=String.format(SEARCH_URL, query);
+
+ //replace all the spaces in the url with "%20"
+ url = url.replaceAll(" ", "%20");
+ InputStream sres = (InputStream)new URL(url).getContent();
+ JSONObject ja = new JSONObject(new JSONTokener(sres));
+ movieIds = new ArrayList<Integer>();
+ Iterator ids_itr = ja.getJSONArray("results").iterator();
+
+ //Iterating through all the results generated by the query and
+ //getting the IDs of each result
+ while(ids_itr.hasNext()){
+ JSONObject id = (JSONObject)ids_itr.next();
+ movieIds.add(id.getInt("id"));
}
- //public static void main(String args[])throws IOException{
- // Search sc = new Search();
- //}
}
-
+}
+