AndroidManifeat.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.iwanghang.volleydemo"> <!-- 网络权限 --> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>MainActivity.java:
package com.iwanghang.volleydemo; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.Volley; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class MainActivity extends Activity implements OnClickListener{ private Button button_volley_1; // 测试按钮一 解析标题和简介 private Button button_volley_2; // 测试按钮二 解析标题 private Button button_volley_3; // 测试按钮三 显示原始json串 private TextView textView_content; // 显示结果 private String title; private String allTitle = ""; private String desc; private String allDesc = ""; private String td; private String allTd = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button_volley_1 = (Button) findViewById(R.id.button_volley_1); // 测试按钮一 解析标题和简介 button_volley_2 = (Button) findViewById(R.id.button_volley_2); // 测试按钮二 解析标题 button_volley_3 = (Button) findViewById(R.id.button_volley_3); // 测试按钮三 显示原始json串 textView_content = (TextView) findViewById(R.id.textView_content); // 显示结果 button_volley_1.setOnClickListener(this); // 测试按钮一 解析标题和简介 点击监听 button_volley_2.setOnClickListener(this); // 测试按钮二 解析标题 点击监听 button_volley_3.setOnClickListener(this); // 测试按钮三 显示原始json串 点击监听 } // /** // * 测试按钮二 解析标题 点击监听 // */ // private void volley_request_title() { // RequestQueue mQueue = Volley.newRequestQueue(this); // // json串: http://www.ytiantuan.com//api.php/index/index.html // JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("http://api.zsreader.com/v2/pub/channel/" + // "list?&page=1&tp=1&size=20", null, // new Response.Listener<JSONObject>() { // @Override // public void onResponse(JSONObject response) { // //textView.setText("正常"); // try { // JSONObject obj1 = new JSONObject(response.toString()); // JSONArray jsonArray = obj1.getJSONArray("data"); // for (int i = 0; i < jsonArray.length(); i++) { // JSONObject obj2 = jsonArray.getJSONObject(i); // Log.e("TAG", obj2.getString("title")); // title = obj2.getString("title"); // allTitle = allTitle + "" + i + "." + title + "\n\n"; // } // textView_content.setText(allTitle); // } catch (JSONException e) { // e.printStackTrace(); // } // } // } // , new Response.ErrorListener() // { // @Override // public void onErrorResponse(VolleyError error) { // Log.e("TCG", error.getMessage(), error); // } // } // ); // mQueue.add(jsonObjectRequest); // } // // /** // * 测试按钮三 显示json串 // */ // private void original() { // RequestQueue requestQueue = Volley.newRequestQueue(this); // // json串: http://www.ytiantuan.com//api.php/index/index.html // String jsonUrl = "http://api.zsreader.com/v2/pub/channel/" + // "list?&page=1&tp=1&size=20"; // JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, // jsonUrl, null, new Response.Listener<JSONObject>() { // @Override // public void onResponse(JSONObject response) { // textView_content.setText(response.toString()); // } // }, new Response.ErrorListener(){ // // @Override // public void onErrorResponse(VolleyError error) { // System.out.println("发生了一个错误!"); // error.printStackTrace(); // } // }); // requestQueue.add(jsonObjectRequest); // } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button_volley_1: // 测试按钮一 解析标题和简介 // 获取Json getJsonResult(new VolleyCallBack() { @Override public void onSuccess(JSONObject result) { // 测试按钮一 解析标题和简介 try { JSONArray jsonArray = result.getJSONArray("data"); for (int i = 0; i < jsonArray.length(); i++) { JSONObject object2 = jsonArray.getJSONObject(i); Log.e("TAG", object2.getString("title")); title = object2.getString("title"); desc = object2.getString("desc"); td = "《" + object2.getString("title") + "》" + object2.getString("desc"); allTd = allTd + "" + i + "." + td + "\n\n"; } textView_content.setText(allTd); } catch (JSONException e) { e.printStackTrace(); } } }); break; case R.id.button_volley_2: // 测试按钮二 解析标题 // 获取Json getJsonResult(new VolleyCallBack() { @Override public void onSuccess(JSONObject result) { // 测试按钮二 解析标题 try { JSONArray jsonArray = result.getJSONArray("data"); for (int i = 0; i < jsonArray.length(); i++) { JSONObject obj2 = jsonArray.getJSONObject(i); Log.e("TAG", obj2.getString("title")); title = obj2.getString("title"); allTitle = allTitle + "" + i + "." + title + "\n\n"; } textView_content.setText(allTitle); } catch (JSONException e) { e.printStackTrace(); } } }); break; case R.id.button_volley_3: // 测试按钮三 显示原始json串 // 获取Json getJsonResult(new VolleyCallBack() { @Override public void onSuccess(JSONObject result) { textView_content.setText(result.toString()); } }); break; } } /** * 获取Json */ public void getJsonResult(final VolleyCallBack volleyCallBack) { // 初始化一个请求队列 RequestQueue requestQueue = com.android.volley.toolbox.Volley.newRequestQueue(this); // json串: http://www.ytiantuan.com//api.php/index/index.html String jsonUrl = "http://api.zsreader.com/v2/pub/channel/list?&page=1&tp=1&size=20"; // 根据给定的URL新建一个请求 JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, jsonUrl, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { // 在这里操作UI组件是安全的,因为响应返回时这个函数会被post到UI线程来执行 // 在这里尽情蹂躏响应的response。 volleyCallBack.onSuccess(response); //成功的回调 //System.out.println("成功返回:"+ response.toString()); } }, new Response.ErrorListener(){ @Override public void onErrorResponse(VolleyError error) { // 出错了怎么办?凉拌!并且在这里拌。 System.out.println("发生了一个错误!"); error.printStackTrace(); } }); // 把这个请求加入请求队列 requestQueue.add(jsonObjectRequest); } /** * VolleyCallback */ public interface VolleyCallBack{ void onSuccess(JSONObject result); } }activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 测试按钮 --> <RelativeLayout android:id="@+id/button_layout" android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:id="@+id/button_volley_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="解析标题和简介"/> <Button android:id="@+id/button_volley_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="解析标题" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <Button android:id="@+id/button_volley_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示原始json串" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </RelativeLayout> <TextView android:id="@+id/textView_content" android:layout_below="@+id/button_layout" android:text="内容" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>