all repos — tmdb-desktop-client @ b1a383bc959a9950a0630b1ec953717495199664

A simple tmdb desktop client written in Java

src/com/oocpro/tmdbdesktop/tmdb/Search.java

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"));
        }
    }
}