RapidSoftwareSolutions / rapidapi-android-sdk

Find, try and connect to APIs easily with the RapidAPI marketplace
http://rapidapi.com
2 stars 4 forks source link

"success" result is a string #4

Open psh opened 8 years ago

psh commented 8 years ago

Example:

    public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Thread t = new Thread() {
                        @Override
                        @SuppressWarnings("unchecked")
                        public void run() {
                            RapidApiConnect connect = new RapidApiConnect("<project>", "<key>");

                            Map<String, Argument> body = new HashMap<>();

                            body.put("rentzestimate", new Argument("data", ""));
                            body.put("zwsId", new Argument("data", "<Zillow web service ID>"));
                            body.put("address", new Argument("data", "2039 yale avenue"));
                            body.put("citystatezip", new Argument("data", "63143"));

                            try {
                                Map<String, Object> response = connect.call("Zillow", "getSearchResults", body);
                                Object success = response.get("success");
                                if (success != null) {
                                    Log.e("Example", "Success is a " + success.getClass().getName());
                                    Log.e("Example", "Success is: \"" + success+"\"");
                                } else {
                                    Log.e("Example", "Oops: "+response);
                                }
                            } catch (Exception e) {
                                Log.e("Example", "Oops", e);
                            }

                        }
                    };
                    t.start();
                }
            });
        }
    }

Result:

    Success is a java.lang.String
    Success is: "{"results":[{"result":[{"zpid":["69228378"],"links":[{"homedetails":["http://www.zillow.com/homedetails/2039-Yale-Ave-Maplewood-MO-63143/69228378_zpid/"],"graphsanddata":["http://www.zillow.com/homedetails/2039-Yale-Ave-Maplewood-MO-63143/69228378_zpid/#charts-and-data"],"mapthishome":["http://www.zillow.com/homes/69228378_zpid/"],"comparables":["http://www.zillow.com/homes/comps/69228378_zpid/"]}],"address":[{"street":["2039 Yale Ave"],"zipcode":["63143"],"city":["Maplewood"],"state":["MO"],"latitude":["38.620956"],"longitude":["-90.311664"]}],"zestimate":[{"amount":[{"_":"133586","$":{"currency":"USD"}}],"last-updated":["11/26/2016"],"oneWeekChange":[{"$":{"deprecated":"true"}}],"valueChange":[{"_":"-645","$":{"duration":"30","currency":"USD"}}],"valuationRange":[{"low":[{"_":"121563","$":{"currency":"USD"}}],"high":[{"_":"140265","$":{"currency":"USD"}}]}],"percentile":["0"]}],"localRealEstate":[{"region":[{"$":{"name":"Maplewood","id":"5799","type":"city"},"zindexValue":["153,900"],"links":[{"overview":["http://www.zillow.com/local-info/MO-Maplewood/r_5799/"],"forSaleByOwner":["http://www.zillow.com/maplewood-mo/fsbo/"],"forSale":["http://www.zillow.com/maplewood-mo/"]}]}]}]},{"zpid":["2112535779"],"links":[{"homedetails":["http://www.zillow.com/homedetails/2039-Yale-Ave-Maplewood-MO-63143/2112535779_zpid/"],"mapthishome":["http://www.zillow.com/homes/2112535779_zpid/"],"comparables":["http://www.zillow.com/homes/comps/2112535779_zpid/"]}],"address":[{"street":["2039 Yale Ave"],"zipcode":["63143"],"city":["Maplewood"],"state":["MO"],"latitude":["38.62077"],"longitude":["-90.31148"]}],"zestimate":[{"amount":[{"_":"134184","$":{"currency":"USD"}}],"last-updated":["11/26/2016"],"oneWeekChange":[{"$":{"deprecated":"true"}}],"valueChange":[{"_":"678","$":{"duration":"30","currency":"USD"}}],"valuationRange":[{"low":[{"_":"111373","$":{"currency":"USD"}}],"high":[{"_":"148944","$":{"currency":"USD"}}]}],"percentile":["0"]}],"localRealEstate":[{"region":[{"$":{"name":"Maplewood","id":"5799","type":"city"},"zindexValue":["153,900"],"links":[{"overview":["http://www.zillow.com/local-info/MO-Maplewood/r_5799/"],"forSaleByOwner":["http://www.zillow.com/maplewood-mo/fsbo/"],"forSale":["http://www.zillow.com/maplewood-mo/"]}]}]}]}]}]}"
psh commented 8 years ago

Note also the JSON that was returned - all of the String values are single item lists of strings. I took a look at the original search result from Zillow - its XML can be obtained using the search

http://www.zillow.com/webservice/GetSearchResults.htm?zws-id=&address=2039+Yale+Ave&citystatezip=63143

Result:

<SearchResults:searchresults xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                             xmlns:SearchResults="http://www.zillow.com/static/xsd/SearchResults.xsd" 
                             xsi:schemaLocation="http://www.zillow.com/static/xsd/SearchResults.xsd 
                             http://www.zillowstatic.com/vstatic/5fa4733/static/xsd/SearchResults.xsd">
    <request>
        <address>2039 Yale Ave</address>
        <citystatezip>63143</citystatezip>
    </request>
    <message>
        <text>Request successfully processed</text>
        <code>0</code>
    </message>
    <response>
        <results>
            <result>
                <zpid>69228378</zpid>
                <links>
                    <homedetails>
                        http://www.zillow.com/homedetails/2039-Yale-Ave-Maplewood-MO-63143/69228378_zpid/
                    </homedetails>
                    <graphsanddata>
                        http://www.zillow.com/homedetails/2039-Yale-Ave-Maplewood-MO-63143/69228378_zpid/#charts-and-data
                    </graphsanddata>
                    <mapthishome>http://www.zillow.com/homes/69228378_zpid/</mapthishome>
                    <comparables>http://www.zillow.com/homes/comps/69228378_zpid/</comparables>
                </links>
                <address>
                    <street>2039 Yale Ave</street>
                    <zipcode>63143</zipcode>
                    <city>Maplewood</city>
                    <state>MO</state>
                    <latitude>38.620956</latitude>
                    <longitude>-90.311664</longitude>
                </address>
                <zestimate>
                    <amount currency="USD">133586</amount>
                    <last-updated>11/26/2016</last-updated>
                    <oneWeekChange deprecated="true"/>
                    <valueChange duration="30" currency="USD">-645</valueChange>
                    <valuationRange>
                        <low currency="USD">121563</low>
                        <high currency="USD">140265</high>
                    </valuationRange>
                    <percentile>0</percentile>
                </zestimate>
                <localRealEstate>
                    <region name="Maplewood" id="5799" type="city">
                        <zindexValue>153,900</zindexValue>
                        <links>
                            <overview>
                                http://www.zillow.com/local-info/MO-Maplewood/r_5799/
                            </overview>
                            <forSaleByOwner>http://www.zillow.com/maplewood-mo/fsbo/</forSaleByOwner>
                            <forSale>http://www.zillow.com/maplewood-mo/</forSale>
                        </links>
                    </region>
                </localRealEstate>
            </result>
            <result>
                <zpid>2112535779</zpid>
                <links>
                    <homedetails>
                        http://www.zillow.com/homedetails/2039-Yale-Ave-Maplewood-MO-63143/2112535779_zpid/
                    </homedetails>
                    <mapthishome>http://www.zillow.com/homes/2112535779_zpid/</mapthishome>
                    <comparables>http://www.zillow.com/homes/comps/2112535779_zpid/</comparables>
                </links>
                <address>
                    <street>2039 Yale Ave</street>
                    <zipcode>63143</zipcode>
                    <city>Maplewood</city>
                    <state>MO</state>
                    <latitude>38.62077</latitude>
                    <longitude>-90.31148</longitude>
                </address>
                <zestimate>
                    <amount currency="USD">134184</amount>
                    <last-updated>11/26/2016</last-updated>
                    <oneWeekChange deprecated="true"/>
                    <valueChange duration="30" currency="USD">678</valueChange>
                    <valuationRange>
                        <low currency="USD">111373</low>
                        <high currency="USD">148944</high>
                    </valuationRange>
                    <percentile>0</percentile>
                </zestimate>
                <localRealEstate>
                    <region name="Maplewood" id="5799" type="city">
                        <zindexValue>153,900</zindexValue>
                        <links>
                            <overview>
                                http://www.zillow.com/local-info/MO-Maplewood/r_5799/
                            </overview>
                            <forSaleByOwner>http://www.zillow.com/maplewood-mo/fsbo/</forSaleByOwner>
                            <forSale>http://www.zillow.com/maplewood-mo/</forSale>
                        </links>
                    </region>
                </localRealEstate>
            </result>
        </results>
    </response>
</SearchResults:searchresults>

This suggests that in addition to the mapping of the "payload" (such that the GSON decode sees it as a string, not an embedded object graph) there's a bug in the original mapping of XML too.

You can apply a work-around on line 103 of RapidApiConnect.java if you don't want to patch the problem server-side - you can do a gson.fromJson(map.get("payload"), ...) but I don't think the client code is the source of the defect.

Bukati commented 8 years ago

The response isn't always a JSON. It depends on the API provider. We decided to always return it as a string and let the user decide how to parse it. We are open for suggestions if you think otherwise

psh commented 7 years ago

I started investigating the SDK using the Nasa API - specifically calling the getPictureOfTheDay endpoint. Substituting that for the example above:

RapidApiConnect connect = new RapidApiConnect("Android_SDK_Tinkering", "<api_key>");
Map<String, Argument> body = new HashMap<>();
body.put("highResolution", new Argument("data", ""));
body.put("date", new Argument("data", ""));
body.put("apiKey", new Argument("data", "<nasa_api_key>"));

try {
    Map<String, Object> response = connect.call("NasaAPI", "getPictureOfTheDay", body);
    Object success = response.get("success");
    if (success != null) {
        Log.e("Example", "Success is a " + success.getClass().getName());
        Log.e("Example", "Success is: \"" + success+"\"");
    } else {
        Log.e("Example", "Oops: "+response);
    }
} catch (Exception e) {
    Log.e("Example", "Oops", e);
}

Yields a different result ("success") type from the call to the Zillow or Spotify endpoints -

Success is a com.google.gson.internal.LinkedTreeMap

Success is: "{date=2016-11-30, explanation=Where do stars form? Many times, stars form in energetic regions where gas and dark dust are pushed around in chaotic mayhem.  Pictured, bright massive stars near the center of W5, the Soul Nebula, are exploding and emitting ionizing light and energetic winds. The outward-moving light and gas push away and evaporate much surrounding gas and dust, but leave pillars of gas behind dense protective knots. Inside these knots, though, stars also form.  The featured image highlights the inner sanctum of W5, an arena spanning about 1,000 light years that is rich in star forming pillars. The Soul Nebula, also cataloged as IC 1848, lies about 6,500 light years away toward the constellation of the Queen of Aethopia (Cassiopeia). Likely, in few hundred million years, only a cluster of the resulting stars will remain. Then, these stars will drift apart., hdurl=http://apod.nasa.gov/apod/image/1611/Soul_Priego_3426.jpg, media_type=image, service_version=v1, title=W5: The Soul of Star Formation, url=http://apod.nasa.gov/apod/image/1611/Soul_Priego_960.jpg}"

For the NASA endpoint, the "payload" comes back the way that the documentation on the website indicates.

In the case of the Spotify and Zillow APIs, on the website, the documentation allows you to drill into the result leading me to think that they would be consistent with the NASA one - a map of maps corresponding with the object graph - but it doesnt get parsed that way. The payload clearly is JSON but it arrives as a String (I am guessing the server wrapped it with extra quotes which stopped Gson from parsing it client side).

In all cases I was able to "explore" the result on the website which is why I was surprised at the inconsistency between the client-side results.

I understand what you are saying - not all of the APIs are created equal. That said, you've clearly put the work in to translate from XML to JSON in the case of Zillow which is why I dont understand the choice to make it a String, and thought it to be a defect.