import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class FlickrFetchr {
private static final String TAG = "FlickrFetchr";
private static final String API_KEY = "";
private static final String FETCH_RECENTS_METHOD = "flickr.photos.getRecent";
private static final String SEARCH_METHOD = "flickr.photos.search";
private static final Uri ENDPOINT = Uri.parse("https://")
.buildUpon()
.appendQueryParameter("api_key", API_KEY)
.appendQueryParameter("format", "json")
.appendQueryParameter("nojsoncallback", "1")
.appendQueryParameter("extras", "url_s")
.build();
List mGalleryItems = new ArrayList<>();
public byte[] getUrlBytes(String urlSpec) throws IOException {
URL url = new URL(urlSpec);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = connection.getInputStream();
if(connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new IOException(connection.getResponseMessage() + ": with" +urlSpec);
}
Log.i(TAG, "getUrlBytes : " +urlSpec);
int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = in.read(buffer)) > 0) {
out.write(buffer, 0, bytesRead);
}
out.close();
return out.toByteArray();
} finally {
connection.disconnect();
}
}
public String getUrlString(String urlSpec) throws IOException {
return new String(getUrlBytes(urlSpec));
}
private String buildUrl(String method, String query) {
Uri.Builder uriBuilder = ENDPOINT.buildUpon()
.appendQueryParameter("method", method);
if(method.equals(SEARCH_METHOD))
uriBuilder.appendQueryParameter("text", query);
return uriBuilder.build().toString();
}
public List fetchRecentPhoto() {
String url = buildUrl(FETCH_RECENTS_METHOD, );
return downloadGalleryItem(url);
}
public List searchPhotos(String query) {
String url = buildUrl(SEARCH_METHOD, query);
return downloadGalleryItem(url);
}
private List downloadGalleryItem(String url) {
try{
String jsonString = getUrlString(url);
Log.i(TAG, "Received JSON: " + jsonString);
JSONObject jsonBody = new JSONObject(jsonString);
parseItem(mGalleryItems, jsonBody);
} catch(JSONException jsone) {
Log.e(TAG, "failed to parse JSON: ", jsone);
} catch(IOException ioe) {
Log.e(TAG, "Failed to fetch items: ", ioe);
}
return mGalleryItems;
}
public void parseItem(List items, JSONObject jsonBody)
throws IOException, JSONException {
JSONObject photoJsonObject = jsonBody.getJSONObject("photos");
JSONArray photoJsonArray = photoJsonObject.getJSONArray("photo");
for (int i = 0; i< photoJsonArray.length(); i++) {
photoJsonObject = photoJsonArray.getJSONObject(i);
GalleryItem item = new GalleryItem();
item.setId(photoJsonObject.getString("id"));
item.setCaption(photoJsonObject.getString("title"));
Log.i(TAG, "id: "+ i +" :" + item.getId());
Log.i(TAG, "title : " + i + ":" + item.getCaption());
if(!photoJsonObject.has("url_s")) {
continue;
}
item.setUrl(photoJsonObject.getString("url_s"));
items.add(item);
}
}
}
-----------------------------------------------------------------------------------------------------
아니 뭐 설명같은거 안해줘도 됨 ㅇㅇ
저기서 코드 빈칸 뚫어놓고 객관식으로 나온다는데 어디서 나올거같음?? (저거 말고 다른것도 있는데 일단 이거만 가져와봄)
참고로 교수님 단순 암기하는 문제 안낸다고 함
ㅅㅂ 쳐보면서 연습하는데 진심 영어타자연습 하는거같네
자바 잘 못해서 더 그런거 같은데 나 2학년떄 어케 자바 A+ 받았징,, 코딩할줄도 모르고 뭔 메소드 이런거도 모르는디
(IP보기클릭)58.143.***.***
최하나
문제는 저런게 15페이지;; 그거 이해하면 좋겠는데 도저히 이해할 가망이 없으니 뭐,, | 18.12.12 01:55 | | |
(IP보기클릭)39.117.***.***