android listview json数据,JSON解析 将数据显示到ListView上

MainActivity的布局:

c53fa27b68aa

image.png

item_xml:

···

android:orientation="vertical" android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/text_title"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="TextView"

android:textColor="@android:color/background_dark"

android:textSize="25sp" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/text_date"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="TextView" />

android:id="@+id/text_src"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:gravity="right"

android:text="TextView" />

···

首先创建联网对象,进行数据连接获取导数据,

在通过Json解析,将数据解析出来,解析出的数据保存到Map集合 中,设置到ListView中:

********具体代码如下********

*********MainActivity中*******************

···

package com.example.wangye.jsontest;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ListView;

import org.json.JSONArray;

import org.json.JSONObject;

import java.io.ByteArrayOutputStream;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import java.util.ArrayList;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

ArrayList> arrayList = new ArrayList<>();

Button bt;

EditText ed;

ListView listView;

MyAdapter adapter;

Handler handler = new Handler(){

@Override

public void handleMessage(Message msg) {

if(msg.what == 1){

adapter = new MyAdapter(MainActivity.this,arrayList);

listView.setAdapter(adapter);

}

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

init();

}

public void init(){

bt = (Button) findViewById(R.id.button);

bt.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

//清空集合

arrayList.clear();

new Thread(){

@Override

public void run() {

getDataByNet();

}

}.start();

}

});

ed = (EditText) findViewById(R.id.editText);

listView = (ListView) findViewById(R.id.listview);

}

public void getDataByNet(){

try {

URL url = new URL("http://op.juhe.cn/onebox/news/query?q="

+ed.getText().toString()+"&dtype=&key=477bff0267dcc1db7dfa8771c77235b2");

HttpURLConnection con = (HttpURLConnection) url.openConnection();

con.setRequestMethod("GET");

con.setDoInput(true);

con.setDoOutput(true);

con.setConnectTimeout(10000);

con.setReadTimeout(10000);

con.connect();

if(con.getResponseCode() == 200){

InputStream is = con.getInputStream();

ByteArrayOutputStream bs = new ByteArrayOutputStream();

byte buffer [] = new byte[512];

int length = -1;

while( (length = is.read(buffer)) != -1){

bs.write(buffer,0,length);

bs.flush();

}

is.close();

bs.close();

parseJson(bs.toString());

}

} catch (Exception e) {

e.printStackTrace();

}

}

public void parseJson(String json){

try {

JSONObject jsonObject = new JSONObject(json);

int code = jsonObject.getInt("error_code");

if(code == 213802){

HashMap map = new HashMap<>();

map.put("title","抱歉,没有搜索到相关信息...");

map.put("pdate","");

map.put("src","");

arrayList.add(map);

}else{

JSONArray jsonArray = jsonObject.getJSONArray("result");

for (int i = 0; i < jsonArray.length(); i++) {

HashMap map = new HashMap<>();

JSONObject jo = jsonArray.getJSONObject(i);

map.put("title",jo.getString("title"));

map.put("pdate",jo.getString("pdate"));

map.put("src",jo.getString("src"));

arrayList.add(map);

}

}

handler.sendEmptyMessage(1);

} catch (Exception e) {

e.printStackTrace();

}

}

}

···

-------------MyAdapter中-------

···

package com.example.wangye.jsontest;

import android.content.Context;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.TextView;

import java.util.ArrayList;

import java.util.HashMap;

/**

Created by wangye on 2018/7/9.

*/

public class MyAdapter extends BaseAdapter {

Context c;

ArrayList> arrayList;

public MyAdapter(Context c, ArrayList> arrayList) {

this.c = c;

this.arrayList = arrayList;

}

@Override

public int getCount() {

return arrayList.size();

}

@Override

public Object getItem(int i) {

return arrayList.get(i);

}

@Override

public long getItemId(int i) {

return 0;

}

@Override

public View getView(int i, View view, ViewGroup viewGroup) {

view = View.inflate(c,R.layout.adapter,null);

TextView txTitle = view.findViewById(R.id.text_title);

txTitle.setText(arrayList.get(i).get("title"));

TextView txDate = view.findViewById(R.id.text_date);

txDate.setText(arrayList.get(i).get("pdate"));

TextView txSrc = view.findViewById(R.id.text_src);

txSrc.setText(arrayList.get(i).get("src"));

return view;

}

}

···

效果图:

c53fa27b68aa

image.png

c53fa27b68aa

image.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值