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

Android Studio中用Volley调用RapidAPI无响应问题求助

Hey there, let's walk through the issues that might be causing your button click to not trigger any response with the Yahoo Finance API call using Volley. Here are the key fixes you need to apply:

1. Fix the URL Escaping Issue

Your URL uses HTML-escaped & characters, but in Java strings you should use plain & instead. Those escaped characters will break the API's ability to parse your request parameters. Update the URL to:

String url="https://apidojo-yahoo-finance-v1.p.rapidapi.com/market/get-quotes?region=CA&lang=en&symbols=VEQT.TO";
2. Add the Required Internet Permission

Volley can't make network requests without the internet permission declared in your AndroidManifest.xml. Add this line outside the <application> tag:

<uses-permission android:name="android.permission.INTERNET"/>

Note: For Android 10+, you don't need extra network state permissions, but this internet permission is non-negotiable.

3. Correct the JSON Parsing Path

The Yahoo Finance RapidAPI response doesn't have a top-level result array directly. The actual structure wraps results inside a quoteResponse object. Update your parsing logic to match:

try {
    JSONObject quoteResponse = response.getJSONObject("quoteResponse");
    JSONArray jsonArray= quoteResponse.getJSONArray("result");
    
    if (jsonArray.length() > 0) {
        JSONObject result=jsonArray.getJSONObject(0);
        // Important: regularMarketPrice is a double, not an int!
        double marketPrice= result.getDouble("regularMarketPrice");
        mTextViewResult.setText(String.format("%.2f", marketPrice));
    } else {
        mTextViewResult.setText("No matching stock found");
    }
} catch (JSONException e) {
    e.printStackTrace();
    mTextViewResult.setText("Parsing error: " + e.getMessage());
}

Using getInt() on a floating-point value like regularMarketPrice would throw an exception, so switching to getDouble() is critical here.

4. Validate Your RapidAPI Key & Improve Error Handling

You've hardcoded "APIKEY" in your headers—replace this with your actual, valid RapidAPI key from your account dashboard. Also, enhance your error listener to give clearer feedback:

@Override
public void onErrorResponse(VolleyError error) {
    String errorMessage;
    if (error.networkResponse != null) {
        errorMessage = "HTTP Error Code: " + error.networkResponse.statusCode;
    } else {
        errorMessage = error.getMessage() != null ? error.getMessage() : "Unknown network error";
    }
    mTextViewResult.setText("Error: " + errorMessage);
    Log.e("VolleyDebug", "API Call Failed: ", error);
}

This will help you spot issues like invalid keys, rate limits, or network timeouts that might be silent before.

5. Double-Check Request Queue Setup

While your queue initialization looks correct (mQueue= Volley.newRequestQueue(this);), make sure you're not accidentally overriding or nullifying the queue elsewhere in your code. For debugging, you can add a tag to your request and log when it's added to the queue:

request.setTag("STOCK_QUOTE_REQUEST");
mQueue.add(request);
Log.d("VolleyDebug", "Request added to queue");

After applying these changes, test your button click again—you should either see the stock price or a clear error message that points you to the remaining issue.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 15:38:13