現在地付近の写真をFlickrで検索して表示
private String getPhotoDataByJson(double lat,double lon){ HttpClient client = new DefaultHttpClient(); HttpParams params = client.getParams(); HttpConnectionParams.setConnectionTimeout(params, 10000); HttpConnectionParams.setSoTimeout(params, 10000); // Uri作成 URI uri = null; try { String apikey = "ABCD1234"; uri = new URI("http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=" + apikey + "&lat=" + lat + "&lon=" + lon + "&radius=1&has_geo=true&format=json&per_page=10&nojsoncallback=1"); } catch (URISyntaxException e) { Log.e("", e.toString()); } //通信開始 HttpUriRequest httpRequest = new HttpGet(uri); HttpResponse httpResponse = null; try { httpResponse = client.execute(httpRequest); } catch (ClientProtocolException e) { Log.e("", "httpRequest:" + e.toString()); } catch (IOException e) { Log.e("", "httpRequest:" + e.toString()); } if (httpResponse == null) { return ""; } //jsonの取得 String json = ""; if (httpResponse != null && httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity httpEntity = httpResponse.getEntity(); try { json = EntityUtils.toString(httpEntity); } catch (IOException e) { Log.e("", "EntityUtils:" + e.toString()); } } return json; }
String json_data = getPhotoDataByJson(35.1234,135.1234); try { JSONObject rootObject = new JSONObject(json_data); JSONObject photoObject = rootObject.getJSONObject("photos"); JSONArray photoArray = photoObject.getJSONArray("photo"); for (int i = 0; i < photoArray.length(); i++) { JSONObject jsonObject = photoArray.getJSONObject(i); String p_id = jsonObject.getString("id"); String p_secret = jsonObject.getString("secret"); String p_server = jsonObject.getString("server"); String p_farm = jsonObject.getString("farm"); String p_title = jsonObject.getString("title"); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); }
private void setFlickr_photo(String p_farm,String p_server,String p_id,String p_seceret){ // iv = new ImageView(this); Uri.Builder ub = new Uri.Builder(); ub.scheme("http"); ub.authority("farm" + p_farm + ".staticflickr.com"); ub.path("/" + p_server + "/" + p_id + "_" + p_seceret + ".jpg"); try { InputStream is = (InputStream) this.fetch(ub.build().toString()); Drawable drawable = Drawable.createFromStream(is, ""); is.close(); iv.setImageDrawable(drawable); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
private String getGeoDataByXml(String api_key,String photo_id){ String latlng = ""; String uri = "http://www.flickr.com/services/rest/?method=flickr.photos.geo.getLocation&format=rest&lang=jp&api_key=" + api_key + "&photo_id=" + photo_id; try{ XmlPullParser xmlPullParser = Xml.newPullParser(); URL url = new URL(uri); URLConnection connection = url.openConnection(); xmlPullParser.setInput(connection.getInputStream(), "UTF-8"); int eventType; while ((eventType = xmlPullParser.next()) != XmlPullParser.END_DOCUMENT) { if (eventType == XmlPullParser.START_TAG) { if ("location".equals(xmlPullParser.getName())){ String lat = xmlPullParser.getAttributeValue(null,"latitude"); String lon = xmlPullParser.getAttributeValue(null,"longitude"); latlng = lat + "," + lon; } } } } } catch (Exception e){ Log.d("XmlPullParserSampleUrl", "Error"); } return latlng; }