src/com/oocpro/tmdbdesktop/tmdb/Search.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.oocpro.tmdbdesktop.tmdb; import com.oocpro.tmdbdesktop.Constants; import java.io.InputStream; import java.io.IOException; import java.net.MalformedURLException; 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 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")); } } }