Android应用开发之Android编程入门-HTTPBackgroundTasks教程
凌雪 2018-10-24 来源 :网络 阅读 1272 评论 0

摘要:本文将带你了解Android应用开发之Android编程入门-HTTPBackgroundTasks教程,希望本文对大家学Android有所帮助。

本文将带你了解Android应用开发之Android编程入门-HTTPBackgroundTasks教程,希望本文对大家学Android有所帮助。


今天开始学习HTTPBackgroundTasks
FlickrFetchr.java
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 =   "REPLACE_ME_WITH_A_REAL_KEY";     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> fetchItems() {         List<galleryitem>   items = new   ArrayList<>();         try   {            String   url = Uri.parse("https://api.flickr.com/services/rest/")                    .buildUpon()                    .appendQueryParameter("method",   "flickr.photos.getRecent")                    .appendQueryParameter("api_key",   API_KEY)                    .appendQueryParameter("format",   "json")                    .appendQueryParameter("nojsoncallback",   "1")                    .appendQueryParameter("extras",   "url_s")                    .build().toString();            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 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);        }    }}</galleryitem></galleryitem></galleryitem>    

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!

本文由 @凌雪 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved