Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package jsonclient; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import java.util.List; import com.google.gson.Gson; import java.util.ArrayList; import java.util.Iterator; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.logging.Level; import java.util.logging.Logger; import places.Place; import places.Places; import places.City; import places.Country; import places.Region; /** * * @author AmirKapsatarov */ //: ?? ? ?( ). // ?? url. // ? ? . //? ? . public class JsonClient { private static String postToURL(String url, String message, DefaultHttpClient httpClient) throws IOException, IllegalStateException, UnsupportedEncodingException, RuntimeException { HttpPost postRequest = new HttpPost(url); StringEntity input = new StringEntity(message); input.setContentType("application/json"); postRequest.setEntity(input); HttpResponse response = httpClient.execute(postRequest); if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); String output; StringBuffer totalOutput = new StringBuffer(); //System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { //System.out.println(output); totalOutput.append(output); } return totalOutput.toString(); } public static String setSlide(String message) throws IOException { String url = "http://something"; DefaultHttpClient httpClient = new DefaultHttpClient(); String response = postToURL(url, message, httpClient); httpClient.getConnectionManager().shutdown(); return response; } public static List<Place> getCountries() { String jsonQuery; String repsonse; Places countries = null; jsonQuery = "{\"data\":{\"action\":\"GetCountryList\", \"param\":{}}}"; List<Place> countriesList = new ArrayList<Place>(); try { repsonse = JsonClient.setSlide(jsonQuery); countries = new Gson().fromJson(repsonse, Places.class); } catch (IOException ex) { System.out.println(ex); } countriesList.addAll(countries.getData().getList()); countriesList.addAll(countries.getData().getPrefferedList()); return countriesList; } public static List<Place> getRegions(String countryId) { String jsonQuery; String repsonse; Places regions = null; jsonQuery = "{\"data\":{\"action\":\"GetRegionList\", \"param\":{\"countryId\":\"" + countryId + "\"}}}"; List<Place> regionsList = new ArrayList<Place>(); try { repsonse = JsonClient.setSlide(jsonQuery); regions = new Gson().fromJson(repsonse, Places.class); } catch (IOException ex) { System.out.println(ex); } regionsList.addAll(regions.getData().getList()); regionsList.addAll(regions.getData().getPrefferedList()); return regionsList; } public static List<Place> getCities(String regionId) { String jsonQuery; String repsonse; Places cities = null; jsonQuery = "{\"data\":{\"action\":\"GetCityList\", \"param\":{\"regionId\":\"" + regionId + "\"}}}"; List<Place> citiesList = new ArrayList<Place>(); try { repsonse = JsonClient.setSlide(jsonQuery); cities = new Gson().fromJson(repsonse, Places.class); } catch (IOException ex) { System.out.println(ex); } citiesList.addAll(cities.getData().getList()); citiesList.addAll(cities.getData().getPrefferedList()); return citiesList; } public static void main(String args[]) { int EPS = 1; List<Place> countries = new ArrayList<Place>(); List<Thread> threads = new ArrayList<Thread>(); Country country; countries = getCountries(); //CountrySearcher countrySearcher = null; Iterator<Place> itrCountry = countries.iterator(); ExecutorService exec = Executors.newFixedThreadPool(4); while (itrCountry.hasNext()) { country = new Country(itrCountry.next()); CountrySearcher cs = new CountrySearcher(country, EPS); threads.add(cs.thread); exec.execute(cs); } exec.shutdown(); for (Thread th : threads) try { th.join(); } catch (InterruptedException ex) { Logger.getLogger(JsonClient.class.getName()).log(Level.SEVERE, null, ex); } } }