Android中基于HTTP的通信技术(5)Google开源库 使用 Volley 实现 JSON 字符串请求

编程入门 行业动态 更新时间:2024-10-25 04:17:30

Android中基于HTTP的通信技术(5)Google开源库 使用 Volley 实现 JSON <a href=https://www.elefans.com/category/jswz/34/1771434.html style=字符串请求"/>

Android中基于HTTP的通信技术(5)Google开源库 使用 Volley 实现 JSON 字符串请求

使用 Volley 实现 JSON 字符串请求,通过极少的代码以及更方便理解的参数完成通信。

(来自极客学院的学习笔记,我是搬运工- -)


  Volley是谷歌开发android平台的网络通信库:更快,更简单,更健壮。
  volley提供的功能;
  1.JSON,图片(异步)
  2.缓存
  3.网络请求的排序
  4.网络请求的优先级处理 
  5.多级别的取消请求 
  6.与Activity生命周期联动(屏幕翻转)

package com.example.usingvolley;import org.json.JSONObject;import android.app.Activity;
import android.os.Bundle;import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);getJSON();}// 获取JSON字符串public void getJSON(){RequestQueue requestQueue = Volley.newRequestQueue(this); //获取Volley的一个请求对象String JSONDateUrl = ".html";JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, JSONDateUrl, null, new Response.Listener<JSONObject>() {public void onResponse(JSONObject response) {System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>response ="+ response);}},new Response.ErrorListener() {public void onErrorResponse(com.android.volley.VolleyError error) {System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>.此数据页有问题");}});//(url, jsonRequest, listener, errorListener)=(请求方式,请求地址,监听事件,错误的监听事件),最后一个参数一次性写完了两个requestQueue.add(jsonObjectRequest);}
}

附上运行图,因为没有json格式的html网址,所以这里返回了错误信息,但是连接已经成功!

Volley.jar下载地址

更多推荐

Android中基于HTTP的通信技术(5)Google开源库 使用 Volley 实现 JSON 字符串请求

本文发布于:2024-03-05 01:16:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1710928.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   开源   通信技术   HTTP   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!