乘车记录数据从网络请求获得
This commit is contained in:
parent
8b98c26223
commit
624f89a682
|
@ -65,7 +65,7 @@ public class Station extends AppCompatActivity {
|
|||
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
|
||||
//请求成功
|
||||
String result = response.body().string();
|
||||
Log.i("test",result);
|
||||
// Log.i("test",result);
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
} catch (InterruptedException e) {
|
||||
|
|
|
@ -18,13 +18,22 @@ import android.widget.TextView;
|
|||
import com.bumptech.glide.Glide;
|
||||
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
|
||||
import com.bumptech.glide.request.RequestOptions;
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import okhttp3.Call;
|
||||
import okhttp3.Callback;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class SubwayHistory extends AppCompatActivity {
|
||||
RecyclerView recyclerView ;
|
||||
MyAdapter myAdapter ;
|
||||
List<history_item.TakeHistoryDTO> history_items = new ArrayList<>();
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
@ -43,6 +52,51 @@ public class SubwayHistory extends AppCompatActivity {
|
|||
}
|
||||
});
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url("https://test04.usemock.com/takeHistory")
|
||||
.get()
|
||||
.build();
|
||||
OkHttpClient okHttpClient = new OkHttpClient();
|
||||
Call call = okHttpClient.newCall(request);
|
||||
//创建了一个线程
|
||||
call.enqueue(new Callback() {
|
||||
@Override
|
||||
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
||||
//请求失败
|
||||
}
|
||||
@Override
|
||||
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
|
||||
//请求成功
|
||||
String result = response.body().string();
|
||||
Log.i("test",result);
|
||||
// try {
|
||||
// Thread.sleep(5000);
|
||||
// } catch (InterruptedException e) {
|
||||
// throw new RuntimeException(e);
|
||||
// }
|
||||
|
||||
|
||||
//所有对于UI控件的操作行为放在主线程中 解决方法:切换到主线程
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Gson gson = new Gson();
|
||||
history_item historyItem = gson.fromJson(result,history_item.class);
|
||||
history_items.addAll(historyItem.takeHistory); // 将解析的数据添加到列表中
|
||||
// stationItemList.addAll(station_item);
|
||||
myAdapter.notifyDataSetChanged();
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public class MyViewHolder extends RecyclerView.ViewHolder {
|
||||
|
@ -80,12 +134,12 @@ public class SubwayHistory extends AppCompatActivity {
|
|||
//设置item中的控件设置值 点击事件
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
|
||||
// history_item historyItem = history_items.get(position);
|
||||
// holder.startTextView.setText(historyItem.start);
|
||||
// holder.finishTextView.setText(historyItem.finish);
|
||||
// holder.moneyTextView.setText(historyItem.money);
|
||||
// holder.timeTextView.setText(historyItem.time);
|
||||
// holder.endTextView.setText(historyItem.end);
|
||||
history_item.TakeHistoryDTO historyItem = history_items.get(position);
|
||||
holder.startTextView.setText(historyItem.start);
|
||||
holder.finishTextView.setText(historyItem.status);
|
||||
holder.moneyTextView.setText(historyItem.price);
|
||||
holder.timeTextView.setText(historyItem.time);
|
||||
holder.endTextView.setText(historyItem.end);
|
||||
|
||||
//TODO 点击事件
|
||||
holder.itemView.setOnClickListener(new View.OnClickListener() {
|
||||
|
@ -102,7 +156,7 @@ public class SubwayHistory extends AppCompatActivity {
|
|||
//item显示条数
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return 20;
|
||||
return history_items.size();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,19 @@
|
|||
package com.hnucm.c25;
|
||||
|
||||
public class history_item {
|
||||
public String start;
|
||||
public String end;
|
||||
public String finish;
|
||||
public String money;
|
||||
public String time;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class history_item {
|
||||
|
||||
@SerializedName("TakeHistory")
|
||||
public List<TakeHistoryDTO> takeHistory;
|
||||
|
||||
public static class TakeHistoryDTO {
|
||||
public String start;
|
||||
public String end;
|
||||
public String status;
|
||||
public String time;
|
||||
public String price;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,17 +6,6 @@
|
|||
android:layout_height="match_parent"
|
||||
tools:context=".SubwayHistory">
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:id="@+id/constraintLayout16"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="40dp"
|
||||
android:background="@color/white"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent">
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:id="@+id/constraintLayout12"
|
||||
android:layout_width="match_parent"
|
||||
|
@ -51,30 +40,15 @@
|
|||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
<androidx.core.widget.NestedScrollView
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/recycleView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginTop="60dp"
|
||||
android:layout_marginBottom="40dp"
|
||||
android:fillViewport="true"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent">
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/recycleView"
|
||||
android:layout_width="409dp"
|
||||
android:layout_height="629dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</androidx.core.widget.NestedScrollView>
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
Reference in New Issue