How should I handle server timeouts and error code responses to an http post in Android App?

后端 未结 3 1708
灰色年华
灰色年华 2021-02-04 18:25

My Android App does http posts to URLs like http://example.com/abc.php?email=abc@xyz.com So the Android App basically talks to PHPs on the server side and receives JSON response

3条回答
  •  有刺的猬
    2021-02-04 18:33

    As Kaj said in the comment, one option is to catch the exception, and perhaps retry or post a failure message.

    For response codes != 200, just read them an do your job inside an if (con.getResponseCode() == 200) block

    Another alternative option, is to use the much nicer Apache classes:

    http://developer.android.com/reference/org/apache/http/package-summary.html

    For a snippet, check BalusC's tutorial here in SO: Using java.net.URLConnection to fire and handle HTTP requests

提交回复
热议问题