Android Volley 发送JSON请求

JsonObjectRequest和JsonArrayRequest(均为JsonRequest的子类),指定一个URL并且从响应中获取JSON对象和数组。

Volley提供以下类用于JSON 请求:

  • JsonArrayRequest - 该请求使用给定url,取回JSONArray响应体;
  • JsonObjectRequest - 该请求使用给定url,取回JSONObject响应体,允许一个可选的JSONObject作为请求提的一个部分被发送。

两个class都以共同的类JsonRequest为基类。下面的代码段将获取JSON返回,并将其显示为UI中的文本:

TextView mTxtDisplay;
ImageView mImageView;
mTxtDisplay = (TextView) findViewById(R.id.txtDisplay);
String url = "http://my-json-feed";

JsonObjectRequest jsObjRequest = new JsonObjectRequest
        (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

    @Override
    public void onResponse(JSONObject response) {
        mTxtDisplay.setText("Response: " + response.toString());
    }
}, new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {
        // TODO Auto-generated method stub

    }
});

// Access the RequestQueue through your singleton class.
MySingleton.getInstance(this).addToRequestQueue(jsObjRequest);

转载于:https://my.oschina.net/u/2453016/blog/994216

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值