获取后台数据明细
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.example.administrator.ydwlxcpt.Adapter.ChongzhiMingxiAdapter;
import com.example.administrator.ydwlxcpt.Bean.ChongzhiMingxi;
import com.example.administrator.ydwlxcpt.Bean.Dingdan;
import com.example.administrator.ydwlxcpt.Bean.Error;
import com.example.administrator.ydwlxcpt.Contast.Contast;
import com.example.administrator.ydwlxcpt.R;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class ChongzhimingxiActivity extends AppCompatActivity {
private static final String TAG = "ChongzhimingxiActivity";
private String url = Contast.Domain + "api/PayToList.ashx?";
private ImageView iv_back;
private ListView listView;
private List<ChongzhiMingxi> chongzhiMingxiList;
private ChongzhiMingxiAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chongzhimingxi);
initViews();
initListView();
}
@Override
protected void onResume() {
super.onResume();
refelash();
}
private void refelash() {
final ProgressDialog pd = new ProgressDialog(ChongzhimingxiActivity.this);
pd.setMessage("拼命加载中...");
pd.show();
FormBody.Builder params = new FormBody.Builder();
params.add("P_PayUTel", Contast.user.getU_Tel());
params.add("keys", Contast.KEYS);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.post(params.build())
.build();
okhttp3.Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
pd.dismiss();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(ChongzhimingxiActivity.this, "服务器繁忙,请稍后再试", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
pd.dismiss();
final String string = response.body().string();
if(response.code() != HttpURLConnection.HTTP_OK){
Toast.makeText(ChongzhimingxiActivity.this, "服务器连接异常,请稍后重试...", Toast.LENGTH_SHORT).show();
}else {
Log.i(TAG, "onResponse: " + string);
if (!TextUtils.isEmpty(string)) {
if (string.contains("ErrorStr")) {
final Error error = JSON.parseObject(string, Error.class);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(ChongzhimingxiActivity.this, error.getErrorStr(), Toast.LENGTH_SHORT).show();
}
});
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
chongzhiMingxiList = JSON.parseArray(string, ChongzhiMingxi.class);
adapter.setData(chongzhiMingxiList);
}
});
}
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(ChongzhimingxiActivity.this, "服务器繁忙,请稍后重试...", Toast.LENGTH_SHORT).show();
}
});
}
}
}
});
}
private void initViews() {
iv_back = (ImageView) findViewById(R.id.iv_chongzhimingxi_back);
iv_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
private void initListView() {
listView = (ListView) findViewById(R.id.lv_chongzhimingxi_listview);
chongzhiMingxiList = new ArrayList<>();
if (chongzhiMingxiList != null) {
adapter = new ChongzhiMingxiAdapter(this, chongzhiMingxiList);
listView.setAdapter(adapter);
}else {
listView.setBackgroundDrawable(getResources().getDrawable(R.drawable.meiyoushuju));
}
}
}
2.xml文件
<?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"
android:background="#f2f2f2"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/titlebg"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_chongzhimingxi_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/back" />
<TextView
android:id="@+id/tv_chongzhimingxi_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/tv_chongzhimingxi_title"
android:textColor="#ffffff"
android:textSize="20sp" />
<Button
android:id="@+id/btn_chongzhimingxi_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@null"
android:text="@string/btn_mycar_tianjiacheliang"
android:textColor="@color/textcolor"
android:textSize="14sp"
android:visibility="invisible" />
</RelativeLayout>
<ListView
android:id="@+id/lv_chongzhimingxi_listview"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
3.适配器
import android.app.Activity;
import android.view.View;
import android.widget.TextView;
import com.example.administrator.ydwlxcpt.Bean.ChongzhiMingxi;
import com.example.administrator.ydwlxcpt.Bean.Youhuiquan;
import com.example.administrator.ydwlxcpt.R;
import java.util.List;
/**
* Created by Administrator on 2017/8/29.
*/
public class ChongzhiMingxiAdapter extends MyBaseAdapter {
public ChongzhiMingxiAdapter(Activity activity, List dataList) {
super(activity, dataList);
}
@Override
public int getItemLayoutResId() {
return R.layout.item_chongzhimingxi_listview;
}
@Override
public Object getViewHolder(View rootView) {
return new ChongzhiMingxiAdapter.ViewHolder(rootView);
}
@Override
public void setItemData(int position, Object dataItem, Object viewHolder) {
final ChongzhiMingxi chongzhiMingxi = (ChongzhiMingxi) dataItem;
//将holder 转为自己holder
ChongzhiMingxiAdapter.ViewHolder myHolder = (ChongzhiMingxiAdapter.ViewHolder) viewHolder;
myHolder.tv_zhanghao.setText(chongzhiMingxi.getP_TakeUTel());
String shijian = chongzhiMingxi.getP_Time();
String replace = shijian.replace("T", " ");
myHolder.tv_shijian.setText(replace);
if(chongzhiMingxi.getP_Type()==1){
myHolder.tv_chongzhifangshi.setText("支付宝付款");
}else if(chongzhiMingxi.getP_Type()==2){
myHolder.tv_chongzhifangshi.setText("微信付款");
}else{
myHolder.tv_chongzhifangshi.setText("其他方式付款");
}
myHolder.tv_zhifujine.setText(""+chongzhiMingxi.getP_PayPrice());
myHolder.tv_daozhangjine.setText(""+(chongzhiMingxi.getP_TakePrice()+chongzhiMingxi.getP_TakePriceAdd()));
}
/**
* ViewHolder 通过构造方法中 实现具体view的绑定的方式 创建一个自实现绑定View的ViewHolder
* Created by bailiangjin on 16/7/5.
*/
public static class ViewHolder {
public final View root;
private TextView tv_zhanghao;
private TextView tv_shijian;
private TextView tv_chongzhifangshi;
private TextView tv_zhifujine;
private TextView tv_daozhangjine;
public ViewHolder(View root) {
this.root = root;
this.tv_zhanghao = (TextView) this.root.findViewById(R.id.tv_chongzhimingxi_listview_zhanghao);
this.tv_shijian = (TextView) this.root.findViewById(R.id.tv_chongzhimingxi_listview_shijian);
this.tv_chongzhifangshi = (TextView) this.root.findViewById(R.id.tv_chongzhimingxi_listview_chongzhifangshi);
this.tv_zhifujine = (TextView) this.root.findViewById(R.id.tv_chongzhimingxi_listview_zhifujine);
this.tv_daozhangjine = (TextView) this.root.findViewById(R.id.tv_chongzhimingxi_listview_daozhangjine);
}
}
}