diff --git a/app/build.gradle b/app/build.gradle index 003df39..f06601f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -24,4 +24,5 @@ dependencies { testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.0.1' compile 'com.android.support:recyclerview-v7:23.0.1' + implementation 'com.android.support.constraint:constraint-layout:1.1.3' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 0be15d1..08d033f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,9 @@ + + + { + + private CityCamActivity activity; + + GetDataTask(CityCamActivity activity) { + this.activity = activity; + updateView(); + } + + + void attachActivity(CityCamActivity activity) { + this.activity = activity; + updateView(); + } + + void detachActivity() { + this.activity = null; + } + + @Override + protected Integer doInBackground(Void... voids) { + try { + URL nearbyUrl = Webcams.createNearbyUrl( + (int) activity.city.latitude, + (int) activity.city.longitude); + HttpsURLConnection connection = (HttpsURLConnection) nearbyUrl.openConnection(); + connection.setRequestProperty("X-RapidAPI-Key", Webcams.RAPID_API_KEY); + + try { + JsonReader reader = new JsonReader(new InputStreamReader((InputStream) connection.getContent())); + List webcams = readWebcams(reader); + if (!webcams.isEmpty()) { + currentWebcam = webcams.get(0); + try { + InputStream in = new URL(currentWebcam.imageUrl).openStream(); + photo = BitmapFactory.decodeStream(in); + } catch (Exception e) { + Log.e(TAG, e.getMessage()); + e.printStackTrace(); + } + updateView(); + } + } catch (Exception e) { + e.printStackTrace(); + } + + + } catch (IOException e) { + e.printStackTrace(); + return 1; + } + return 0; + } + + private List readWebcams(JsonReader reader) throws IOException { + List webcams = new ArrayList<>(); + + reader.beginObject(); + while (reader.hasNext()) { + String name = reader.nextName(); + if (name.equals("result")) { + reader.beginObject(); + while (reader.hasNext()) { + name = reader.nextName(); + if (name.equals("webcams")) { + reader.beginArray(); + while (reader.hasNext()) { + Log.i(TAG, "NAME IN ARRAy: " + reader.peek().name()); + webcams.add(readWebcam(reader)); + } + reader.endArray(); + } else { + reader.skipValue(); + } + } + reader.endObject(); + } else { + reader.skipValue(); + } + } + reader.endObject(); + + return webcams; + } + + private Webcam readWebcam(JsonReader reader) throws IOException { + Webcam webcam = new Webcam(); + + try { + reader.beginObject(); + } catch (Exception e) { + e.printStackTrace(); + Log.i(TAG, reader.toString()); + } + while (reader.hasNext()) { + String name = reader.nextName(); + + switch (name) { + case "id": + webcam.id = reader.nextLong(); + break; + case "image": + webcam.imageUrl = readImageUrl(reader); + break; + case "location": + reader.beginObject(); + while (reader.hasNext()) { + name = reader.nextName(); + switch (name) { + case "city": + webcam.city = reader.nextString(); + break; + case "region": + webcam.region = reader.nextString(); + break; + case "latitude": + webcam.latitude = reader.nextDouble(); + break; + case "longitude": + webcam.longitude = reader.nextDouble(); + break; + default: + reader.skipValue(); + break; + } + } + reader.endObject(); + break; + default: + reader.skipValue(); + break; + } + } + reader.endObject(); + + Log.i(TAG, webcam.toString()); + return webcam; + } + + private String readImageUrl(JsonReader reader) throws IOException { + String result = null; + + reader.beginObject(); + while (reader.hasNext()) { + String name = reader.nextName(); + if (name.equals("current")) { + reader.beginObject(); + while (reader.hasNext()) { + name = reader.nextName(); + if (name.equals("preview")) { + result = reader.nextString(); + } else { + reader.skipValue(); + } + } + reader.endObject(); + } else { + reader.skipValue(); + } + } + reader.endObject(); + return result; + } + + private void updateView() { + activity.runOnUiThread( + new Runnable() { + @Override + public void run() { + if (currentWebcam != null) { + activity.cityTextView.setText(currentWebcam.city); + activity.regionTextView.setText(currentWebcam.region); + activity.latTextView.setText(String.format("%.2f", currentWebcam.latitude)); + activity.longTextView.setText(String.format("%.2f", currentWebcam.longitude)); + + if (photo != null) { + activity.camImageView.setImageBitmap(photo); + activity.progressView.setVisibility(View.GONE); + } + } + } + } + ); + + } + + } + + private static class Webcam { + + long id; + + String city; + + String region; + + String imageUrl; + + double latitude; + + double longitude; + + @Override + public String toString() { + return "Webcam{" + "id=" + id + + ", city='" + city + '\'' + + ", region='" + region + '\'' + + ", imageUrl='" + imageUrl + '\'' + + ", latitude=" + latitude + + ", longitude=" + longitude + + '}'; + } + } } diff --git a/app/src/main/java/ru/android_2019/citycam/webcams/Webcams.java b/app/src/main/java/ru/android_2019/citycam/webcams/Webcams.java index 9d732ba..b760b58 100644 --- a/app/src/main/java/ru/android_2019/citycam/webcams/Webcams.java +++ b/app/src/main/java/ru/android_2019/citycam/webcams/Webcams.java @@ -10,34 +10,26 @@ */ public final class Webcams { - // Зарегистрируйтесь на http://ru.webcams.travel/developers/ - // и вставьте сюда ваш devid - private static final String DEV_ID = "Ваш devid"; + public static final String RAPID_API_KEY = "1b6b2bf409msh301a3b7eec5d75fp14ec10jsne9d8f9ba2511"; - private static final String BASE_URL = "http://api.webcams.travel/rest"; + private static final String BASE_URL = "https://webcamstravel.p.rapidapi.com/webcams"; - private static final String PARAM_DEVID = "devid"; - private static final String PARAM_METHOD = "method"; - private static final String PARAM_LAT = "lat"; - private static final String PARAM_LON = "lng"; - private static final String PARAM_FORMAT = "format"; + private static final String PATH_NEARBY = "list/nearby"; - private static final String METHOD_NEARBY = "wct.webcams.list_nearby"; + private static final String PARAM_LANGUAGE = "lang"; + private static final String PARAM_SHOW = "show"; - private static final String FORMAT_JSON = "json"; + private static final int RADIUS = 100; /** * Возвращает URL для выполнения запроса Webcams API для получения * информации о веб-камерах рядом с указанными координатами в формате JSON. */ - public static URL createNearbyUrl(double latitude, double longitude) + public static URL createNearbyUrl(int latitude, int longitude) throws MalformedURLException { - Uri uri = Uri.parse(BASE_URL).buildUpon() - .appendQueryParameter(PARAM_METHOD, METHOD_NEARBY) - .appendQueryParameter(PARAM_LAT, Double.toString(latitude)) - .appendQueryParameter(PARAM_LON, Double.toString(longitude)) - .appendQueryParameter(PARAM_DEVID, DEV_ID) - .appendQueryParameter(PARAM_FORMAT, FORMAT_JSON) + Uri uri = Uri.parse(BASE_URL + "/" + PATH_NEARBY + "=" + latitude + "," + longitude + "," + RADIUS).buildUpon() + .appendQueryParameter(PARAM_LANGUAGE, "ru") + .appendQueryParameter(PARAM_SHOW, "webcams:image,location") .build(); return new URL(uri.toString()); } diff --git a/app/src/main/res/layout/activity_city_cam.xml b/app/src/main/res/layout/activity_city_cam.xml index 17fab12..e085c2e 100644 --- a/app/src/main/res/layout/activity_city_cam.xml +++ b/app/src/main/res/layout/activity_city_cam.xml @@ -1,11 +1,13 @@ - + + + + + + + + + + + \ No newline at end of file