版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/juer2017/article/details/79228836
先上效果截图:
1.获取服务器json数据
/**
* 获取session_id
*/
sprfMain = getSharedPreferences("counter", Context.MODE_PRIVATE);
final String session_id = sprfMain.getString("session_id", "");
Log.d(session_id, "获取登录时的session_id");
new Thread(new Runnable() {
@Override
public void run() {
try {
@SuppressWarnings("deprecation")
String PATH = "这里是服务器的地址";
URL url = new URL(PATH);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//配置参数
connection.setRequestMethod("GET");
connection.setRequestProperty("Cookie", session_id);//设置sessionid
connection.setConnectTimeout(TIME_OUT);
connection.setReadTimeout(TIME_OUT);
//打开链接
connection.connect();
//获取状态码
int responseCode = connection.getResponseCode();
if (200 == responseCode) {
//获取返回值
InputStream inputStream = connection.getInputStream();
//将字节流输入流转换为字符串
data = StreamUtils.inputSteam2String(inputStream);
Log.d(data, "请求账单时服务器返回的数据");
handler.obtainMessage(RESULT_OK, data).sendToTarget();
} else {
handler.obtainMessage(RESULT_CANCELED, responseCode).sendToTarget();
}
} catch (MalformedURLException e) {
e.printStackTrace();
handler.obtainMessage(RESULT_CANCELED, e.getMessage()).sendToTarget();
} catch (IOException e) {
e.printStackTrace();
handler.obtainMessage(RESULT_CANCELED, e.getMessage()).sendToTarget();
}
}
}).start();
这里只解析了需要的数据进行遍历到ListView中
2.布局
主布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="44dp">
<ImageView
android:id="@+id/iv_back"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="10dp"
android:src="@drawable/back" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal"
android:text="交易记录"
android:textColor="#080808"
android:textSize="20sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/ll_tip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="129.5dp"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="60dp"
android:src="@drawable/wzd" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:text="无交易记录"
android:textColor="#7f7e7e"
android:textSize="15sp" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="#f5f5f5" />
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="6dp"
android:divider="#f5f5f5"/>
</RelativeLayout>
</LinearLayout>
子布局
<?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="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:text="买入"
android:textColor="@color/Qblue"
android:textSize="12sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="泛财富\t翻倍宝1号"
android:textColor="@color/black"
android:textSize="12sp" />
<TextView
android:id="@+id/tv_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="02-01\t10:30"
android:textSize="12sp" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tv_money"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100000元"
android:textColor="@color/black"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看详情"
android:textColor="@color/Qblue"
android:textSize="10sp" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
3.解析与查看详情
private String data;
private final static int TIME_OUT = 1000;//超时时间
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case RESULT_OK:
ll_tip.setVisibility(View.GONE);//如果有数据则隐藏提示内容
try {
JSONArray arr = new JSONArray((String) msg.obj);
for (int i = 0; i < arr.length(); i++) {
JSONObject temp = (JSONObject) arr.get(i);
Data data = new Data();
data.setBuyMoney(temp.getString("buyMoney"));
data.setProductId(temp.getString("productId"));
data.setProductInstanceId(temp.getString("productInstanceId"));
data.setCreateDate(temp.getString("createDate"));
datas.add(data);
}
lv.setAdapter(new MyAdapter());
} catch (JSONException e) {
e.printStackTrace();
}
break;
case RESULT_CANCELED:
Toast.makeText(getBaseContext(), "服务器繁忙……", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
};
/**
* ListView子项的点击事件
*
* @param adapterView
* @param view
* @param i
* @param l
*/
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
startActivity(new Intent(this, OrderDataActivity.class));
Bundle bundle = new Bundle();
bundle.putString("productId", datas.get(i).getProductId());
bundle.putString("money", datas.get(i).getBuyMoney());
bundle.putString("productInstanceId", datas.get(i).getProductInstanceId());
try {
JSONObject obj = new JSONObject(datas.get(i).getCreateDate());
bundle.putString("createDate", (obj.getInt("month") + 1) + "-" + obj.getString("date") + " " +
obj.getString("hours") + ":" + obj.getString("minutes") + ":" + obj.getString("seconds"));
} catch (JSONException e) {
e.printStackTrace();
}
Intent intent = new Intent();
intent.putExtras(bundle);
intent.setClass(ZDActivity.this, OrderDataActivity.class);
startActivity(intent);
}
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return datas.size();
}
@Override
public Object getItem(int position) {
return datas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
view = View.inflate(ZDActivity.this, R.layout.item_zd, null);
TextView money = view.findViewById(R.id.tv_money);
TextView name = view.findViewById(R.id.tv_name);
TextView date = view.findViewById(R.id.tv_date);
money.setText(datas.get(position).getBuyMoney());//显示金额
try {//显示时间
JSONObject obj = new JSONObject(datas.get(position).getCreateDate());
date.setText((obj.getInt("month") + 1) + "-" + obj.getString("date") + " " +
obj.getString("hours") + ":" + obj.getString("minutes") + ":" + obj.getString("seconds"));
} catch (JSONException e) {
e.printStackTrace();
}
if ("201801250101".equals(datas.get(position).getProductId())) {
name.setText("泛财富 日忝宝");//显示产品名称
}
return view;
}
}
详情布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="44dp">
<ImageView
android:id="@+id/iv_back"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="10dp"
android:src="@drawable/back" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal"
android:text="记录详情"
android:textColor="#080808"
android:textSize="20sp" />
</RelativeLayout>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="泛财富\t翻倍宝1号"
android:textSize="12sp" />
<TextView
android:id="@+id/tv_money"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="100000.00元"
android:textColor="@color/black"
android:textSize="24sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="份额待确认"
android:textColor="@color/Qblue" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="43dp"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/iv_order" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="9dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二天确认份额,开始计算收益"
android:textSize="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="第三天,查看收益"
android:textSize="10dp" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_marginTop="30dp"
android:background="@color/view_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="43dp"
android:layout_marginTop="14dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="买入信息"
android:textColor="@color/black" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="43dp"
android:layout_marginTop="14dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="买入产品"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_name2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="43dp"
android:layout_marginTop="14dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="买入金额"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_money2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="43dp"
android:layout_marginTop="14dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="付款方式"
android:textColor="@color/black" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="账户余额"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="43dp"
android:layout_marginTop="14dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="买入时间"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="10sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_marginTop="20dp"
android:background="@color/view_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="43dp"
android:layout_marginTop="14dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="订单号"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_productInstanceId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="10sp" />
</LinearLayout>
<Button
android:id="@+id/btn_buy"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="30dp"
android:background="@drawable/shape_btn"
android:text="再买一笔"
android:textColor="@color/white" />
</LinearLayout>
解析的参数传递到详情页面的java代码
package com.fb.hckjfb.activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.fb.hckjfb.R;
import org.w3c.dom.Text;
/**
* 订单详情
*/
public class OrderDataActivity extends BaseActivity implements View.OnClickListener {
private ImageView iv_back;
private TextView tv_money, tv_money2, tv_name, tv_name2, tv_productInstanceId, tv_date;
private Button btn_buy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_data);
initView();
Bundle bundle = getIntent().getExtras();
String money = bundle.getString("money");
String productId = bundle.getString("productId");
String productInstanceId = bundle.getString("productInstanceId");
String createDate = bundle.getString("createDate");
tv_money.setText(money + "元");//显示投资金额
tv_money2.setText(money + "元");
if ("201801250101".equals(productId)) {
tv_name.setText("泛财富 日忝宝");//显示产品名称
tv_name2.setText("泛财富 日忝宝");
}
tv_productInstanceId.setText(productInstanceId);//显示订单号
tv_date.setText(createDate);//显示投资日期
}
@Override
protected int getLayoutID() {
return R.layout.activity_order_data;
}
@Override
protected void initListener() {
}
@Override
protected void initView() {
iv_back = findViewById(R.id.iv_back);//返回
tv_money = findViewById(R.id.tv_money);//投资金额
tv_name = findViewById(R.id.tv_name);//产品名称
tv_name2 = findViewById(R.id.tv_name2);//产品名称2
tv_money2 = findViewById(R.id.tv_money2);//投资金额2
tv_productInstanceId = findViewById(R.id.tv_productInstanceId);//订单号
btn_buy = findViewById(R.id.btn_buy);//再买一笔按钮
tv_date = findViewById(R.id.tv_date);//投资日期
iv_back.setOnClickListener(this);
btn_buy.setOnClickListener(this);
}
@Override
protected void initData() {
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.iv_back://返回
startActivity(new Intent(this, ZDActivity.class));
finish();
break;
case R.id.btn_buy://再买一笔
String name = tv_name.getText().toString();
if ("泛财富 日忝宝".equals(name)) {
startActivity(new Intent(this, QRTZFDActivity.class));
}
break;
default:
break;
}
}
@Override
public void onBackPressed() {//返回键
super.onBackPressed();
startActivity(new Intent(this, ZDActivity.class));
finish();
}
}
主要代码都在这里了,这里是从代码里拿出来的一部分,如果是新建的项目不要忘记写权限哦。