Android开发-Volley-解析Json使用方法-2-完整Demo-AndroidStudio

来源:互联网 发布:linux awk命令详解 编辑:程序博客网 时间:2024/05/17 04:03

Android开发-Volley-解析Json使用方法-2-完整Demo-AndroidStudio
区别于

Android开发-Volley-解析Json使用方法-完整Demo-AndroidStudio
但是在解析json的方法里setText并不好

好的方法请看:

Android开发-Volley-解析Json使用方法-4-完整Demo-AndroidStudio


Android开发-Volley-解析Json使用方法-3-完整Demo-AndroidStudio 是一个联系过渡用法


AndroidManifest.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.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;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 Volley4Json volley4Json;    private Ceshi_bean ceshi_bean;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        volley4Json = new Volley4Json(getApplicationContext());        ceshi_bean = volley4Json.getCeshi_bean();        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串 点击监听    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.button_volley_1: // 测试按钮一 解析标题和简介                volley4Json.volley_request_title_desc(textView_content);                //textView_content.setText("解析标题和简介\n\n" + ceshi_bean.getAllTd());                break;            case R.id.button_volley_2: // 测试按钮二 解析标题                volley4Json.volley_request_title(textView_content);                //textView_content.setText("解析标题\n\n" + ceshi_bean.getAllTitle());                break;            case R.id.button_volley_3: // 测试按钮三 显示原始json串                volley4Json.original(textView_content);                textView_content.setText(ceshi_bean.getOriginal_content());                break;        }    }}
Ceshi_bean.java:

package com.iwanghang.volleydemo;public class Ceshi_bean {    private String title;    private String allTitle;    private String desc;    private String allDesc = "";    private String td;    private String allTd = "";    private String original_content;    public String getOriginal_content() {        return original_content;    }    public void setOriginal_content(String original_content) {        this.original_content = original_content;    }    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }    public String getAllTitle() {        return allTitle;    }    public void setAllTitle(String allTitle) {        this.allTitle = allTitle;    }    public String getDesc() {        return desc;    }    public void setDesc(String desc) {        this.desc = desc;    }    public String getAllDesc() {        return allDesc;    }    public void setAllDesc(String allDesc) {        this.allDesc = allDesc;    }    public String getTd() {        return td;    }    public void setTd(String td) {        this.td = td;    }    public String getAllTd() {        return allTd;    }    public void setAllTd(String allTd) {        this.allTd = allTd;    }}
Volley4Json.java:

package com.iwanghang.volleydemo;import android.content.Context;import android.util.Log;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 org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;public class Volley4Json {    private Context contxt;    public Ceshi_bean ceshi_bean = new Ceshi_bean();    private String title;    private String allTitle = "";    private String desc;    private String allDesc = "";    private String td;    private String allTd = "";    private String original_content;    public Volley4Json(Context context){        this.contxt = context;    }    public Ceshi_bean getCeshi_bean() {        return ceshi_bean;    }    /**     * 解析标题和简介     */    public void volley_request_title_desc(final TextView textview) {        RequestQueue mQueue = com.android.volley.toolbox.Volley.newRequestQueue(contxt);        // 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";                        desc = obj2.getString("desc");                        allDesc = allDesc + "" + i + "." +  desc  + "\n\n";                        td = "《" + obj2.getString("title") + "》" + obj2.getString("desc");                        allTd = allTd + "" + i + "." +  td  + "\n\n";                    }                    //ceshi_bean.setAllTitle(allTitle);                    //ceshi_bean.setAllDesc(allDesc);                    //ceshi_bean.setAllTd(allTd);                    //textview.setText(allTitle);                    //textview.setText(allDesc);                    textview.setText(allTd);                } catch (JSONException e) {                    e.printStackTrace();                }            }        }, new Response.ErrorListener()        {            @Override            public void onErrorResponse(VolleyError error) {                Log.e("TCG", error.getMessage(), error);            }        }        );        mQueue.add(jsonObjectRequest);    }    /**     * 解析标题     */    public void volley_request_title(final TextView textview) {        RequestQueue mQueue = com.android.volley.toolbox.Volley.newRequestQueue(contxt);        // 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";                        desc = obj2.getString("desc");                        allDesc = allDesc + "" + i + "." +  desc  + "\n\n";                        td = "《" + obj2.getString("title") + "》" + obj2.getString("desc");                        allTd = allTd + "" + i + "." +  td  + "\n\n";                    }                    //ceshi_bean.setAllTitle(allTitle);                    //ceshi_bean.setAllDesc(allDesc);                    //ceshi_bean.setAllTd(allTd);                    textview.setText(allTitle);                    //textview.setText(allDesc);                    //textview.setText(allTd);                } catch (JSONException e) {                    e.printStackTrace();                }            }        }, new Response.ErrorListener()        {            @Override            public void onErrorResponse(VolleyError error) {                Log.e("TCG", error.getMessage(), error);            }        }        );        mQueue.add(jsonObjectRequest);    }    /**     * 显示json串     */    public void original(final TextView textview) {        RequestQueue requestQueue = com.android.volley.toolbox.Volley.newRequestQueue(contxt);        // 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>() {        //JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(jsonUrl, null, new Response.Listener<JSONObject>() {            @Override            public void onResponse(JSONObject response) {                original_content = response.toString();                //ceshi_bean.setOriginal_content(original_content);                textview.setText(original_content);            }        }, new Response.ErrorListener(){            @Override            public void onErrorResponse(VolleyError error) {                System.out.println("发生了一个错误!");                error.printStackTrace();            }        });        requestQueue.add(jsonObjectRequest);    }}
activitry_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>

1 0