简体   繁体   中英

how to determine if a web service is up and running

i was trying to call the following web service from my android app, it hung then completed without returning the result:

web service: http://androidexample.com/media/webservice/JsonReturn.php

However when I clicked on the link, it worked fine - the json file displayed. yet it would not work in my app..

but now, it works fine now in my android app, perhaps it was temporarily down is what I am guessing. How can I know if a web service is up and running for an android app to consume ?

Typically, web services are designed to have a status page that can return status text or a HTTP return code to indicate service status.

If it doesn't have that, you can design a function to periodically do a very basic request with a known result to determine state. This is much better than doing a simple ping.

If it was down it would most likely show a HTML error page, which your app would try to parse, which would cause an error.

I had a similar issue, because I needed to know if the user was returning HTML or the correct JSON, to do this I created the ArrayList I was about to use outside of the try/catch of the parse area. You should do the same if you are using a string.

What I mean is, use:

ArrayList<Something> arrayList = new ArrayList<Something>();
String testString = ""; instead of String testString = null;

I was using only ArrayList<Something> arrayList; at one point which is incorrect. If the user then returns HTML, you won't get an error, the user will simply return an empty arraylist or empty string.

You can then plan for that and show some sort of error message. This way you only need one network request but you can still plan for getting the data back, and the server being down.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM