搜索

搜索
1.AndroidManifest. xml

<?xml version="1.0" encoding="utf-8"?>


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".PhotoGalleryActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

2.FlickrFetchr. java
package com.bignerdranch.android.photogallery;

import android.net.Uri;
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 = "6f3d929a9cdf223655e55837d4c82b1c";
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("http://www.win4000.com/wallpaper_detail_55318.html")
        .buildUpon()
        .appendQueryParameter("api_key", API_KEY)
        .appendQueryParameter("format", "json")

        .appendQueryParameter("nojsoncallback", "1")
        .appendQueryParameter("extras", "url_s")
        .build();

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

public List<GalleryItem> fetchRecentPhotos() {
    String url = buildUrl(FETCH_RECENTS_METHOD, null);
    return downloadGalleryItems(url);
}

public List<GalleryItem> searchPhotos(String query) {
    String url = buildUrl(SEARCH_METHOD, query);
    return downloadGalleryItems(url);
}

private List<GalleryItem> downloadGalleryItems(String url) {
    List<GalleryItem> items = new ArrayList<>();

    try {
        String jsonString = getUrlString(url);
        Log.i(TAG, "Received JSON: " + jsonString);
        JSONObject jsonBody = new JSONObject(jsonString);
        parseItems(items, jsonBody);
    } catch (IOException ioe) {
        Log.e(TAG, "Failed to fetch items", ioe);
    } catch (JSONException je) {
        Log.e(TAG, "Failed to parse JSON", je);
    }

    return items;
}

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

private void parseItems(List<GalleryItem> items, JSONObject jsonBody)
        throws IOException, JSONException {

    JSONObject photosJsonObject = jsonBody.getJSONObject("photos");
    JSONArray photoJsonArray = photosJsonObject.getJSONArray("photo");

    for (int i = 0; i < photoJsonArray.length(); i++) {
        JSONObject photoJsonObject = photoJsonArray.getJSONObject(i);

        GalleryItem item = new GalleryItem();
        item.setId(photoJsonObject.getString("id"));
        item.setCaption(photoJsonObject.getString("title"));

        if (!photoJsonObject.has("url_s")) {
            continue;
        }

        item.setUrl(photoJsonObject.getString("url_s"));
        items.add(item);
    }
}

}

3.GalleryItem. java
package com.bignerdranch.android.photogallery;

public class GalleryItem {
private String mCaption;
private String mId;
private String mUrl;

public String getCaption() {
    return mCaption;
}

public void setCaption(String caption) {
    mCaption = caption;
}

public String getId() {
    return mId;
}

public void setId(String id) {
    mId = id;
}

public String getUrl() {
    return mUrl;
}

public void setUrl(String url) {
    mUrl = url;
}

@Override
public String toString() {
    return mCaption;
}

}

4.在这里插入图片描述
注意:详细步骤请查看书本上面,因本人软件只能做到此步,若需要其他的步骤请自行解决哦。代码下面截图里面的程序是接着程序走的,只是不知道为什么编辑博客时是代码发出来代码部分变成了图片的样子但是没关系都是从那个位置连接往下的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值