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

Android中Volley未发起网络连接问题排查求助

Volley网络请求无响应问题排查求助

我尝试使用Volley从互联网请求数据,以下是测试代码(代码较为粗糙,仅用于验证Volley功能,后续会优化):

public static Option parseJSON(Context context, String stockTicker, String strikePrice, String expiration) {
        final String apikey = "XXXX"; //key removed just for StackOverflow, but it works
        String ticker = "&symbol=";
        final String baseURL = "https://api.tdameritrade.com/v1/marketdata/quotes?";
        Option option = new Option();

        try {
            SimpleDateFormat simpleDate = new SimpleDateFormat("MM/dd/yyyy");
            SimpleDateFormat simpleDateforRequest = new SimpleDateFormat("MMddyy");
            String formattedDate = simpleDateforRequest.format(simpleDate.parse(expiration));
            String fullTicker = stockTicker + "_" + formattedDate + "C" + strikePrice;
            ticker = ticker + fullTicker;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        final String url = baseURL + apikey + ticker;

        RequestQueue queue = Volley.newRequestQueue(context);

        JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                String jsonObject = response.toString();
                Log.d("JSONRequestResponse", "Response: " + jsonObject);
            }
        }, error -> Log.d("JSONRequestResponse", "Error: " + error.toString()));

        queue.add(request);

        Log.d("JSON", "Request" + request + " URL: " + url);
        return option; //method doesn't create an option yet, but the problem comes well before this.
    }

目前的问题是JsonArrayRequest中的Log.d均未触发,最后一条日志显示为空数组[],推测Volley未尝试发起连接。

我已进行如下排查:

  • 尝试使用JsonObjectRequest和StringRequest替代JsonArrayRequest
  • 已在Manifest中添加<uses-permission android:name="android.permission.INTERNET"/>权限,也尝试添加了ACCESS_NETWORK_STATE权限
  • 最终生成的URL可正常访问,能返回正确的JSON数据
  • Gradle版本为1.2.1,已是最新版本

目前我仅需验证Volley能否获取JSON数据,尚未处理返回对象的逻辑,完全不清楚问题所在,恳请帮助。

更新:
正如预期,应用似乎无法连接互联网。我使用以下方法检测网络可用性,返回结果为false:

public static boolean isNetworkAvailable() {
    Log.d("CheckPoint", "isNetworkAvailable first line");
    final boolean[] availability = {false};
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                availability[0] = InetAddress.getByName("www.google.com").isReachable(5000);
            } catch (UnknownHostException e) {
                Log.d("CheckPoint", "isNetworkAvailable UnknownHost!");
            } catch (IOException e) {
                Log.d("CheckPoint", "isNetworkAvailable IOException!");
            }
        }
    }).start();

    Log.d("CheckPoint", "Availability: " + availability[0]);
    return availability[0];
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 08:25:15