这个是我做的电子书阅读器其中的一个测试页面
也没有去加图片什么的
JSON 字符串格式
[ {"idd":"50736","id":"19147735","lx":"Ebook","mc":"冷笑话","dx":"32.74","rq":"2020-04-17"}, {"idd":"50735","id":"70347734","lx":"Ebook","mc":"九阳神功","dx":"2443.37","rq":"2020-04-17"}, {"idd":"50734","id":"64147733","lx":"Ebook","mc":"神魔九变","dx":"2784.28","rq":"2020-04-17"}, {"idd":"50733","id":"58147732","lx":"Ebook","mc":"神墓","dx":"6109.03","rq":"2020-04-17"}, {"idd":"50732","id":"37947731","lx":"Ebook","mc":"极品无赖","dx":"1757.93","rq":"2020-04-17"}, {"idd":"50731","id":"17047730","lx":"Ebook","mc":"凌天传说","dx":"4909.78","rq":"2020-04-17"}, {"idd":"50730","id":"44047729","lx":"Ebook","mc":"花间曲","dx":"1914.91","rq":"2020-04-17"} ]
list_item.xml(这里面TV4我保存书名的序列号 不想让用户看到 设置成隐藏了)
<?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" > <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text="111222333" /> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text=" 111333222" android:layout_toRightOf="@id/tv1" /> <TextView android:id="@+id/tv3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text=" 999-111" android:layout_below="@id/tv2" android:layout_toRightOf="@id/tv1" /> <TextView android:id="@+id/tv4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text=" 999-11www1" android:layout_below="@id/tv2" android:layout_toRightOf="@id/tv3" android:visibility="invisible" /> </RelativeLayout>
MainActivity
package com.jinesc.http; import android.os.Handler; import android.os.Message; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; public class MainActivity extends AppCompatActivity implements View.OnClickListener { public static final int SHOW_RESPONSE=0;//用于更新操作 private Button sendRequest; private ListView lv; private String JSONstr; //用于处理和发送消息的Hander private Handler handler=new Handler(){ public void handleMessage(Message msg){ //如果返现msg.what=SHOW_RESPONSE,则进行制定操作,如想进行其他操作,则在子线程里将SHOW_RESPONSE改变 switch (msg.what){ case SHOW_RESPONSE: String response=(String)msg.obj; //进行UI操作,将结果赋值到JSONSTR JSONstr=response; } } }; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //sendRequest=(Button)findViewById(R.id.send_request); //responseText=(TextView)findViewById(R.id.response_text); //sendRequest.setOnClickListener(this); sendRequestWithHttpURLConnection(); //读取数据 lv = (ListView) findViewById(R.id.list_view); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) { TextView tt= arg0.getChildAt(arg2).findViewById(R.id.tv4); //获得listview中TV4 TEXTVIEW setTitle("第"+arg2+"行"+tt.getText().toString());//设置标题栏显示点击的行 } }); } @Override public void onClick(View v) { if(v.getId()==R.id.tolistv){ sendRequestWithHttpURLConnection(); //读取数据 lv = (ListView) findViewById(R.id.list_view); /*定义一个动态数组*/ ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>(); /*在数组中存放数据*/ JSONArray jsonArray = null; try { jsonArray = new JSONArray(JSONstr); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String mc = jsonObject.getString("mc"); String lx = jsonObject.getString("lx"); String dx = jsonObject.getString("dx"); String idd = jsonObject.getString("idd"); HashMap<String, Object> map = new HashMap<String, Object>(); map.put("mc", mc+" "); map.put("lx", lx+">>"); map.put("dx", dx+"KB "); map.put("idd", idd); listItem.add(map); } } catch (JSONException e) { e.printStackTrace(); } SimpleAdapter mSimpleAdapter = new SimpleAdapter(this,listItem,//需要绑定的数据 R.layout.list_item,//每一行的布局 //动态数组中的数据源的键对应到定义布局的View中 new String[] {"lx","mc", "dx","idd"}, new int[] {R.id.tv1,R.id.tv2,R.id.tv3,R.id.tv4}); lv.setAdapter(mSimpleAdapter);//为ListView绑定适配器 } } private void sendRequestWithHttpURLConnection(){ //开启线程来发起网络请求 new Thread(new Runnable() { @Override public void run() { HttpURLConnection connection=null; try{ URL url=new URL("这里是网址内容 已经隐藏了!!!"); connection=(HttpURLConnection)url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(8000); connection.setReadTimeout(8000); InputStream in=connection.getInputStream(); //下面对获取到的输入流进行读取 BufferedReader bufr=new BufferedReader(new InputStreamReader(in)); StringBuilder response=new StringBuilder(); String line=null; while((line=bufr.readLine())!=null){ response.append(line); } Message message=new Message(); message.what=SHOW_RESPONSE; //将服务器返回的数据存放到Message中 message.obj=response.toString(); handler.sendMessage(message); }catch(Exception e){ e.printStackTrace(); }finally { if(connection!=null){ connection.disconnect(); } } } }).start(); } }
发表评论