volley与java版本_java-在Android中使用Volley从JSON解析数据

我尝试解析“ https://api.instagram.com/v1/media/popular?client_id=”中的JSON数据

clientId;或任何其他网址,以多种方式!使用了几个JSONParsers,教程,读者..一切,但是仍然无法从这些url中获取任何信息.现在,我正在使用Volley库,但仍然无法正常工作,这是我的代码以及您需要的一切,如果有人有任何想法,请分享.

public void LoadPictures() {

mRequestQueue = Volley.newRequestQueue(this);

mRequestQueue.add(new JsonObjectRequest(urlInst, null,

new Listener() {

@Override

public void onResponse(JSONObject response) {

try {

parseJSON(response);

} catch (JSONException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}, new ErrorListener() {

@Override

public void one rrorResponse(VolleyError error) {

error.printStackTrace();

}

}));

这是我的parseJSON方法:

private void parseJSON(JSONObject json) throws JSONException{

// JSONObject value = json.getJSONObject("value");

JSONArray items = json.getJSONArray("data");

for(int i=0;i

JSONObject c=(JSONObject) items.get(i);

JSONObject user = c.getJSONObject("user");

String name= user.getString("username");

JSONObject img=c.getJSONObject("images");

JSONObject thum=img.getJSONObject("thumbnail");

String urlOfPic = thum.getString("url");

PhotoInst photoData=new PhotoInst (i, urlOfPic, name);

photos.add(photoData);

}

这是我应该得到的JSON数据:

"data": [{

"type": "image",

"users_in_photo": [],

"filter": "Gotham",

"tags": [],

"comments": { ... },

"caption": {

"created_time": "1296656006",

"text": "ãã¼ãâ¥ã¢ããªå§ãã¦ä½¿ã£ã¦ã¿ãã(^^)",

"from": {

"username": "cocomiin",

"full_name": "",

"type": "user",

"id": "1127272"

},

"id": "26329105"

},

"likes": {

"count": 35,

"data": [{

"username": "mikeyk",

"full_name": "Kevin S",

"id": "4",

"profile_picture": "..."

}, {...subset of likers...}]

},

"link": "http://instagr.am/p/BV5v_/",

"user": {

"username": "cocomiin",

"full_name": "Cocomiin",

"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1127272_75sq_1296145633.jpg",

"id": "1127272"

},

"created_time": "1296655883",

"images": {

"low_resolution": {

"url": "http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_6.jpg",

"width": 306,

"height": 306

},

"thumbnail": {

"url": "http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_5.jpg",

"width": 150,

"height": 150

},

"standard_resolution": {

"url": "http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_7.jpg",

"width": 612,

"height": 612

}

},

"id": "22518783",

"location": null

},

当我尝试放入随机Toast来查看问题出在哪里时,我可以看到我的方法LoadPictures中的onResponse根本没有被调用?我在哪里失败?我只是监督一些小事情还是其他事情吗?

解决方法:

@Mate-从json中可以看到,您正在获取JsonArray,即“数据”.因此,将您的侦听器更改为Listener< JSONArray>. whihc确保它返回JSONArray对象.结果,您的onResponse现在将变为

@Override

public void onResponse(JSONArray response) {

try {

parseJSON(response);

} catch (JSONException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

让我知道这个是否奏效.

标签:android-volley,json,java,android

来源: https://codeday.me/bug/20191122/2062522.html

Volley是一个优秀的安卓开源网络访问工具,这里包含了volley jar包和源码,版本是2015.07.28的1.0.18版本。更多资料可以参见volley的github地址: https://github.com/mcxiaoke/android-volley 1.0.18 2015.07.28 merge upstream, process response bodies only when present tweak getCacheKey(), using method and original url, fix #67 #78 add RedirectError, wrong exception for http 301 and 302, fix #51 make NetworkResponse Serializable, fix #53 create NetworkError using IOException, not using null response 1.0.17 2015.07.13 merge upstream, stop logging slow requests outside of debug logs merge pr #64, fix leaking the last request object 1.0.16 2015.05.18 fix duplicate retry, change DEFAULT_MAX_RETRIES to 0 merge pr, fix NegativeArraySizeException merge upstream, Use a BufferedOutputStream to read and write cache 1.0.15 2015.03.18 add two missing constructors for JsonArrayRequest and JsonObjectRequest add unique identifier for Request 1.0.14 2015.03.17 add more constructors for JsonArrayRequest and JsonObjectRequest update and fix gradle build, using new build.gradle 1.0.13 2015.03.16 merge pr, added constructor to JSONArrayRequest 1.0.12 2015.03.12 merge upstream, fix ImageLoader.getCacheKey() merge upstream, Fix broken DiskBasedCache merge upstream, Modify header parser to handle must-revalidate. 1.0.11 2015.03.03 merge upstream, Add a RequestFinishedListener to RequestQueue. merge upstream, Change the default character encoding for JSON responses to UTF-8 1.0.10 2014.12.30 merge upstream, Use the view size and scale type to restrict NIV requests. merge pr, Add a getImageURL method to NetworkImageView merge pr, Add the options of setting DiskBasedCache sizes in Volley.java 1.0.9 2014.11.26 merge upstream, Fix deprecation warnings (now errors in 0.14.4) w/ gradle. 1.0.8 2014.11.07 merge upstream, Metric for network time and getBackoffMultiplier() 1.0.7 2014.10.13 merge upstream, Add locale to HttpHeaderParserTest.rfc1123Date(long millis) merge upstream, Copy cache header for 304 response 1.0.6 2014.09.10 merge pr, fixed bug where Disk cache misses for redirects merge upstream, Fix crash/OOM in DiskBasedCache merge upstream, allow use of custom ImageRequests in ImageLoader 1.0.5 2014.06.18 merge upstream, Fixes bug in PATCH method. Sets the method before setting the body. update gradle and build tools version, fix build 1.0.4 2014.05.04 merge upstream, Guard against NullPointerException currently occurring in Volley when a Request is given a url whose host is null. 1.0.3 2014.04.01 merge upstream, ImageLoader: setError after null check 1.0.2 2014.02.28 merge upstream, Use the view size to restrict NIV requests. merge upstream, Fix generic type parameter for RequestQueue.add(Request) merge pr, added support for handling 301 and 302 http redirects using standalone deploy gradle build file and script 1.0.1 2014.02.13 add gradle build support, add readme, release using gradle 1.0.0 2014.01.03 create volley mirror at github, release first version
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值