Android Volley 发送JSON请求

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

Volley提供如下类用于JSON 请求:数组

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

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

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);
相关文章
相关标签/搜索