Android:如何将Volley获取的JSON数据展示到多个TextView中
解决Volley获取JSON后拆分字段到独立TextView的问题
Hey there! 作为刚入门一周的编程新手,你已经能通过Volley成功获取JSON数据了,这已经迈出了很棒的一步!接下来咱们一步步把需要的字段拆分出来,分别展示到对应的TextView里。
第一步:准备布局中的TextView
首先你需要在activity_main.xml里添加三个用于展示指定字段的TextView,给它们设置唯一的id,比如:
<!-- 可保留原有TextView,新增三个目标TextView用于展示拆分后的数据 --> <TextView android:id="@+id/tv_delivery_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="DeliveryId: "/> <TextView android:id="@+id/tv_pick_location" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="PickLocation: "/> <TextView android:id="@+id/tv_quantity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Quantity: "/>
第二步:修改Java代码解析JSON并赋值
接下来修改你的MainActivity代码,主要做三件事:
- 声明并初始化新增的三个TextView
- 在
onResponse方法里解析JSON字符串,提取目标字段 - 将解析出的字段值设置到对应的TextView中
完整修改后的代码示例:
import org.json.JSONException; import org.json.JSONObject; // 记得导入JSON相关的包 public class MainActivity extends AppCompatActivity { private TextView textView; // 原有TextView(不需要可删除) private TextView tvDeliveryId, tvPickLocation, tvQuantity; // 新增的三个TextView private RequestQueue queue; private static final String URL = "你的请求URL"; // 替换成你的实际请求地址 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化所有TextView textView = findViewById(R.id.text); tvDeliveryId = findViewById(R.id.tv_delivery_id); tvPickLocation = findViewById(R.id.tv_pick_location); tvQuantity = findViewById(R.id.tv_quantity); queue = Volley.newRequestQueue(this); StringRequest request = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() { @Override public void onResponse(String response) { // 解析JSON字符串,必须用try-catch捕获异常 try { // 将返回的字符串转为JSON根对象 JSONObject rootObj = new JSONObject(response); // 提取嵌套的Delivery对象,再获取里面的DeliveryId JSONObject deliveryObj = rootObj.getJSONObject("Delivery"); int deliveryId = deliveryObj.getInt("DeliveryId"); tvDeliveryId.setText("DeliveryId: " + deliveryId); // 直接提取根对象里的PickLocation字段 String pickLocation = rootObj.getString("PickLocation"); tvPickLocation.setText("PickLocation: " + pickLocation); // 提取Quantity字段(注意是double类型) double quantity = rootObj.getDouble("Quantity"); tvQuantity.setText("Quantity: " + quantity); // 原有整块显示的代码可保留或删除 textView.setText(response.toString()); Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_LONG).show(); } catch (JSONException e) { // 捕获解析异常,避免程序崩溃 e.printStackTrace(); Toast.makeText(MainActivity.this, "数据解析出错啦", Toast.LENGTH_SHORT).show(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d("error", error.toString()); Toast.makeText(MainActivity.this, "请求失败,请检查网络", Toast.LENGTH_SHORT).show(); } }); queue.add(request); } }
关键细节提醒
- 异常处理不能少:JSON解析过程中很容易出现格式不匹配、字段不存在的情况,用
try-catch包裹能避免程序直接崩溃,还能快速定位问题。 - 嵌套字段要逐层取:
DeliveryId是在Delivery这个嵌套对象里的,必须先拿到Delivery对象,再从中提取字段,不能直接从根对象取。 - 字段名大小写敏感:代码里写的字段名(比如
DeliveryId)必须和JSON返回的完全一致,大小写错了就会找不到字段哦。
额外注意事项
确保你的AndroidManifest.xml里已经添加了网络权限,否则Volley请求会失败:
<uses-permission android:name="android.permission.INTERNET" />
内容的提问来源于stack exchange,提问作者bjoeres
相关产品推荐
相关产品推荐

