Android源码+Springboot服务器
https://download.csdn.net/download/xiaoxin0630/11106276
总体UI的代码 适配器 重新封装
package dialog.com.binglian.ui;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import dialog.com.binglian.R;
import dialog.com.binglian.entity.Road;
import dialog.com.binglian.utils.JsonAdapter;
public class UrlActivity extends AppCompatActivity {
private Spinner spinner;
private ListView listView;
private BufferedReader bufferedReader;
public List<Road> lists=null;
private Button button;
private JsonAdapter jsonAdapter;
final String host="http://binglian066.natapp1.cc/hlv";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_url);
listView=(ListView) findViewById(R.id.lv_list1);
spinner=(Spinner) findViewById(R.id.spinner);
lists=new ArrayList<>();
new Thread(new Runnable() {
@Override
public void run() {
try {
parseJsonData(readParse(host));
System.out.println("dada:"+lists.get(1).getLukou());
for(Road news:lists){
Log.i("lukou:",news.getLukou());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
//这里休眠是为了让子线程结束 lists才有值
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("dare1"+lists.get(1).getLukou());
jsonAdapter=new JsonAdapter(this,lists);
listView.setAdapter(jsonAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, final int position, long id) {
button= findViewById(R.id.btn_paixu);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取资源下的数组中的Spinner中order下的值
getResources().getStringArray(R.array.order);
getResources().getStringArray(R.array.order);
Log.d("233",""+getResources().getStringArray(R.array.order)[position]);
}
});
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
/**
* 从指定的URL中获取数组
* @param urlPath
* @return
* @throws Exception
*/
public static String readParse(String urlPath) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
URL url = new URL(urlPath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream inStream = conn.getInputStream();
while ((len = inStream.read(data)) != -1) {
outStream.write(data, 0, len);
}
inStream.close();
return new String(outStream.toByteArray());//通过out.Stream.toByteArray获取到写的数据
}
private void parseJsonData(String string){
lists = new ArrayList<>();
try {
JSONArray jsonArray=new JSONArray(string);
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
Road road = new Road();
road.setLukou(jsonObject.getString("lukou"));
road.setRed(jsonObject.getString("red"));
road.setYellow(jsonObject.getString("yellwo"));
road.setGreen(jsonObject.getString("green"));
lists.add(road);
// String lukou=jsonObject.getString("lukou");
// String red=jsonObject.getString("red");
// String yellwo=jsonObject.getString("yellwo");
// String green=jsonObject.getString("green");
// System.out.println("lukou:"+lukou+",red:"+red+",yellow:"+yellwo+",greend:"+green);
}
Log.i("Test", "OK,数据存储完成");
Log.i("Test", "List长度为:"+lists.size());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
封装的适配器
package dialog.com.binglian.utils;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
import dialog.com.binglian.R;
import dialog.com.binglian.entity.Road;
public class JsonAdapter extends BaseAdapter {
private List<Road> list;
private Context context;
private LayoutInflater inflater;
public JsonAdapter(Context context,List<Road> list){
this.list=list;
this.context=context;
inflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder=null;
if(convertView ==null){
convertView=inflater.inflate(R.layout.item,null);
holder=new Holder(convertView);
convertView.setTag(holder);
}else{
holder=(Holder) convertView.getTag();
}
Road road=list.get(position);
holder.Rd.setText(road.getLukou());
holder.red.setText(road.getRed());
holder.yello.setText(road.getYellow());
holder.green.setText(road.getGreen());
return convertView;
}
class Holder{
private TextView Rd,red,yello,green;
public Holder(View view){
Rd=(TextView) view.findViewById(R.id.tv_road);
red=(TextView) view.findViewById(R.id.tv_red);
yello=(TextView) view.findViewById(R.id.tv_yellowlight);
green=(TextView) view.findViewById(R.id.tv_greenlight);
}
}
}
json传过来的实体类
package dialog.com.binglian.entity;
public class Road {
private String lukou;
private String red;
private String yellow;
private String green;
public String getLukou() {
return lukou;
}
public void setLukou(String lukou) {
this.lukou = lukou;
}
public String getRed() {
return red;
}
public void setRed(String red) {
this.red = red;
}
public String getYellow() {
return yellow;
}
public void setYellow(String yellow) {
this.yellow = yellow;
}
public String getGreen() {
return green;
}
public void setGreen(String green) {
this.green = green;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:entries="@array/order"/>
<Button
android:id="@+id/btn_paixu"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="排序"/>
</LinearLayout>
<ListView
android:id="@+id/lv_list1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
listView的item文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_road"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="路口"/>
<TextView
android:id="@+id/tv_red"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="红灯时长/s"/>
<TextView
android:id="@+id/tv_greenlight"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="绿灯时长/s"/>
<TextView
android:id="@+id/tv_yellowlight"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="黄灯时长/s"/>
</LinearLayout>
</LinearLayout>
Spinner下拉内容 xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="order">
<item>红灯将序</item>
<item>绿灯将序</item>
</string-array>
</resources>
首先需要获取json数据
json地址是自己写的一个springboot服务器,
当时试了很多获取的方法 但是 虽然能获取到 解析不出来 这就是一个问题
我这里使用了这个方法就可以解析到
/**
* 从指定的URL中获取数组
* @param urlPath
* @return
* @throws Exception
*/
public static String readParse(String urlPath) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
URL url = new URL(urlPath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream inStream = conn.getInputStream();
while ((len = inStream.read(data)) != -1) {
outStream.write(data, 0, len);
}
inStream.close();
return new String(outStream.toByteArray());//通过out.Stream.toByteArray获取到写的数据
}
解析json数据
获取传过来的数据 进行解析
JsonArray 传到JSONArray
然后再用jsonArray.getJSONObject一一取出来
然后再给Road对象 在添加到list中
private void parseJsonData(String string){
lists = new ArrayList<>();
try {
JSONArray jsonArray=new JSONArray(string);
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
Road road = new Road();
road.setLukou(jsonObject.getString("lukou"));
road.setRed(jsonObject.getString("red"));
road.setYellow(jsonObject.getString("yellwo"));
road.setGreen(jsonObject.getString("green"));
lists.add(road);
// String lukou=jsonObject.getString("lukou");
// String red=jsonObject.getString("red");
// String yellwo=jsonObject.getString("yellwo");
// String green=jsonObject.getString("green");
// System.out.println("lukou:"+lukou+",red:"+red+",yellow:"+yellwo+",greend:"+green);
}
Log.i("Test", "OK,数据存储完成");
Log.i("Test", "List长度为:"+lists.size());
} catch (JSONException e) {
e.printStackTrace();
}
}
然后这个是子线程中调用方法
parseJsonData(readParse(host));
这就是传递URL地址获取json数据 然后解析数据
这里我当时因为没有学过Android的线程 发现 只要出了Thread list数据获取不到 所需要需要把数据更新到主线程中
new Thread(new Runnable() {
@Override
public void run() {
try {
parseJsonData(readParse(host));
System.out.println("dada:"+lists.get(1).getLukou());
for(Road news:lists){
Log.i("lukou:",news.getLukou());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
我这里用的是休眠的方法
然后我这里设置了设配器需要显示出来 把list传递到自定义的适配器
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("dare1"+lists.get(1).getLukou());
jsonAdapter=new JsonAdapter(this,lists);
listView.setAdapter(jsonAdapter);
自定义适配器
inflater=Layoutflater.from(context);
这里自定义构造方法 传递参数进来
public JsonAdapter(Context context,List<Road> list){
this.list=list;
this.context=context;
inflater=LayoutInflater.from(context);
}
这里是判断是否第一次缓存运行
Holder holder=null;
if(convertView ==null){
convertView=inflater.inflate(R.layout.item,null);
holder=new Holder(convertView);
convertView.setTag(holder);
}else{
holder=(Holder) convertView.getTag();
}
package dialog.com.binglian.utils;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
import dialog.com.binglian.R;
import dialog.com.binglian.entity.Road;
public class JsonAdapter extends BaseAdapter {
private List<Road> list;
private Context context;
private LayoutInflater inflater;
public JsonAdapter(Context context,List<Road> list){
this.list=list;
this.context=context;
inflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder=null;
if(convertView ==null){
convertView=inflater.inflate(R.layout.item,null);
holder=new Holder(convertView);
convertView.setTag(holder);
}else{
holder=(Holder) convertView.getTag();
}
Road road=list.get(position);
holder.Rd.setText(road.getLukou());
holder.red.setText(road.getRed());
holder.yello.setText(road.getYellow());
holder.green.setText(road.getGreen());
return convertView;
}
class Holder{
private TextView Rd,red,yello,green;
public Holder(View view){
Rd=(TextView) view.findViewById(R.id.tv_road);
red=(TextView) view.findViewById(R.id.tv_red);
yello=(TextView) view.findViewById(R.id.tv_yellowlight);
green=(TextView) view.findViewById(R.id.tv_greenlight);
}
}
}
下拉列表
监听事件
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, final int position, long id) {
button= findViewById(R.id.btn_paixu);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getResources().getStringArray(R.array.order);
getResources().getStringArray(R.array.order);
Log.d("233",""+getResources().getStringArray(R.array.order)[position]);
}
});
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});