1、JsonObjectRequest用法
用法和StringRequest基本相同,主要分为3步:
(1)、实例化一个RequestQueue对象
(2)、设置JsonObjectRequest对象参数,并将JsonObjectRequest对象加入RequestQueue队列
(3)、执行start()方法
public class JsonActivity extends Activity { private TextView mTvShow; private RequestQueue requestQueue; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_json); initView(); } public void initView(){ mTvShow = (TextView) findViewById(R.id.tv_json); requestQueue = Volley.newRequestQueue(getBaseContext()); requestQueue.add(jsonObjectRequest); requestQueue.start(); } /** JsonObject请求*/ public JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("http://m.weather.com.cn/data/101010100.html", null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { // TODO Auto-generated method stub if(response != null){ mTvShow.setText(response.toString()); }else{ Log.e("JsonObjectRequest", "Error"); } } }, new Response.ErrorListener(){ @Override public void onErrorResponse(VolleyError error) { // TODO Auto-generated method stub Log.e("JsonObjectRequest", error.getMessage()); } }); }
2、不要忘记加入网络访问权限,服务器代码大家自己写吧,这里的不对。
<uses-permission android:name="android.permission.INTERNET"/>
3、参考博文:
http://blog.csdn.net/xyz_lmn/article/details/12165391