【问题标题】:Volley in Android not attempting to connectAndroid 中的 Volley 未尝试连接
【发布时间】:2023-01-03 06:55:02
【问题描述】:

我一直试图让 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 中的 logd 都没有被触发,最后的只是一个空数组(“[]”),让我觉得 Volley 没有尝试连接。

  • 我也尝试过使用 JsonObjectRequest 和 StringRequest
  • 我已经将“<uses-permission android:name="android.permission.INTERNET'/> 添加到清单中,并尝试使用“ACCESS_NETWORK_STATE”
  • 最终 URL 有效。我点击了它以获得最终的 logd,它将我带到一个包含正确信息的 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!");
            }
        }
    });

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

【问题讨论】:

    标签: java android json android-volley


    【解决方案1】:

    你从来没有打电话

    requestQueue.start()
    

    虽然我不得不说——除非你真的需要一些只有 Volley 给你的功能,Retrofit 是一个更容易使用的库,它避免了 Volley 的一些问题(Volley 可能会导致内存问题,大量响应或多个未完成的请求,因为它需要将整个响应保存为内存中的字符串,而不是将其流式传输到临时文件)。

    【讨论】:

    • 感谢您的评论,但我在 .add 之前和之后分别调用了 start,但没有任何改变。还是空白。我可能会切换到 Retrofit 看看是否更容易
    猜你喜欢
    • 2018-08-04
    • 2013-08-30
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多