如何获取Wolfram Simple API返回结果图片的URL?
Hey there! I’ve worked with the Wolfram Simple API before, so let’s walk through how to grab that image URL you need to show in your ImageView.
First, let’s recap how the Simple API works by default: when you send a request (like https://api.wolframalpha.com/v1/simple?appid=YOUR_APP_ID&i=2%2B2), it returns a tiny HTML page that contains a single <img> tag pointing to the generated result image. Your goal is to parse that HTML and pull out the src attribute of that image tag.
Here’s a step-by-step breakdown:
Ensure your API request returns HTML (not raw image data)
By default, the Simple API returns HTML, but double-check you aren’t using theformat=imageparameter (that would send raw binary image data instead of HTML with the URL). Stick to the default response format.Parse the HTML response to extract the image URL
You’ll need an HTML parser to pull out thesrcvalue. For Android, the Jsoup library is perfect for this. Here’s how to implement it:Add the Jsoup dependency to your project (for Gradle, add this to your app-level
build.gradle):implementation 'org.jsoup:jsoup:1.17.2'After making your API call and receiving the HTML string response, use Jsoup to parse it:
// Assume 'apiResponse' is the HTML string you got from the API Document doc = Jsoup.parse(apiResponse); Element imageElement = doc.selectFirst("img"); if (imageElement != null) { String imageUrl = imageElement.attr("src"); // Now load this URL into your ImageView using a library like Glide or Picasso Glide.with(this) .load(imageUrl) .into(yourImageView); } else { // Handle case where no image tag was found (check API request for errors) }
Verify the URL validity
The extractedsrcshould be a full absolute URL (likehttps://www4b.wolframalpha.com/Calculate/MSP/MSP1234...) that you can directly use to load the image. If it’s a relative URL (unlikely with Wolfram’s API), prepend the base Wolfram domain (https://www4b.wolframalpha.com) to it.
Quick troubleshooting tip: If you aren’t getting the HTML response, double-check your API key (appid) is correct and your request parameters are properly URL-encoded (e.g., spaces or special characters in your query should be encoded).
内容的提问来源于stack exchange,提问作者Neha

