You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何向URL提交JSON数据并解析响应在TextView显示?Volley是否适用?

当然适用!Volley完美匹配你的场景

Volley本身就是为Android轻量网络请求和JSON解析设计的,像你给出的这种结构规整的JSON响应,用Volley处理起来非常顺畅,完全能满足你的需求——提交JSON数据、接收响应并解析后显示在TextView里。

下面给你一套完整的实现步骤和代码示例:

1. 先添加Volley依赖

在你的app模块的build.gradle(Module级别)中加入Volley依赖:

implementation 'com.android.volley:volley:1.2.1'

2. 实现请求、解析与UI更新

在你的Activity或Fragment中,按照以下代码实现:

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONObject;

// 假设你在Activity里,先获取你的TextView实例
TextView yourTextView = findViewById(R.id.your_text_view_id);

// 初始化Volley请求队列
RequestQueue requestQueue = Volley.newRequestQueue(this);
String targetUrl = "你的目标请求URL";

// 如果你需要提交JSON数据,先构造要发送的JSONObject(不需要的话传null即可)
JSONObject postJsonData = new JSONObject();
try {
    postJsonData.put("param1", "value1");
    postJsonData.put("param2", "value2");
} catch (Exception e) {
    e.printStackTrace();
}

// 创建JSON请求
JsonObjectRequest jsonRequest = new JsonObjectRequest(
        Request.Method.POST,  // 根据接口要求选POST或GET
        targetUrl,
        postJsonData,  // 要提交的JSON数据,不需要就传null
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                // 解析响应JSON并更新TextView
                try {
                    boolean isSuccess = response.getBoolean("isSuccess");
                    String returnCode = response.getString("returnCode");
                    String returnMessage = response.getString("returnMessage");
                    // 可以根据需求自由拼接显示内容
                    String displayContent = String.format(
                            "请求结果:%s\n返回码:%s\n返回信息:%s",
                            isSuccess ? "成功" : "失败",
                            returnCode,
                            returnMessage
                    );
                    // Volley的回调在主线程,直接更新TextView即可,不用额外切换线程
                    yourTextView.setText(displayContent);
                } catch (Exception e) {
                    e.printStackTrace();
                    yourTextView.setText("JSON解析出错:" + e.getMessage());
                }
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                yourTextView.setText("请求失败:" + (error.getMessage() != null ? error.getMessage() : "未知错误"));
            }
        }
);

// 将请求加入队列执行
requestQueue.add(jsonRequest);

几个实用的小提示

  • 对于resource和errors这种可能为null的字段,建议用optXXX方法解析,比如response.optJSONObject("resource"),这样字段为null时不会抛出异常,代码更健壮。
  • 如果你的请求需要重复使用,可以把RequestQueue做成单例,避免重复创建队列造成资源浪费。
  • 别忘了在AndroidManifest.xml中添加网络权限:
    <uses-permission android:name="android.permission.INTERNET" />
    

内容的提问来源于stack exchange,提问作者Rajesh Donepudi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:30:02