【问题标题】:http API Calling handle status code common functionhttp API调用句柄状态码常用函数
【发布时间】:2023-02-09 17:50:08
【问题描述】:

把它投到

重写有趣的 httpErrorHandle(e: Throwable) { val error = e 作为 ANError errorMessage(活动!!,错误) }

【问题讨论】:

    标签: android function kotlin http http-status-codes


    【解决方案1】:
     val error = e as ANError
     showErrorMessage(activity, error.errorCode)
    
    
    HTTP status code below function check 
    Pass argument - Context and HTTP status code
    
    fun showErrorMessage(activity: Activity, Code: Int) {
        when (Code) {
            0 -> showCustomToastCode(activity, activity.getString(R.string.code_500))
            100 -> showCustomToastCode(activity, activity.getString(R.string.code_100))
            101 -> showCustomToastCode(activity, activity.getString(R.string.code_101))
            103 -> showCustomToastCode(activity, activity.getString(R.string.code_103))
            200 -> showCustomToastCode(activity, activity.getString(R.string.code_200))
            201 -> showCustomToastCode(activity, activity.getString(R.string.code_201))
            202 -> showCustomToastCode(activity, activity.getString(R.string.code_202))
            203 -> showCustomToastCode(activity, activity.getString(R.string.code_203))
            204 -> showCustomToastCode(activity, activity.getString(R.string.code_204))
            205 -> showCustomToastCode(activity, activity.getString(R.string.code_205))
            206 -> showCustomToastCode(activity, activity.getString(R.string.code_206))
            300 -> showCustomToastCode(activity, activity.getString(R.string.code_300))
            301 -> showCustomToastCode(activity, activity.getString(R.string.code_301))
            302 -> showCustomToastCode(activity, activity.getString(R.string.code_302))
            303 -> showCustomToastCode(activity, activity.getString(R.string.code_303))
            304 -> showCustomToastCode(activity, activity.getString(R.string.code_304))
            307 -> showCustomToastCode(activity, activity.getString(R.string.code_307))
            308 -> showCustomToastCode(activity, activity.getString(R.string.code_308))
            400 -> showCustomToastCode(activity, activity.getString(R.string.code_400))
            401 -> {
                unAuthorizedPopUpView(activity)
            }
            402 -> showCustomToastCode(activity, activity.getString(R.string.code_402))
            403 -> showCustomToastCode(activity, activity.getString(R.string.code_403))
            404 -> showCustomToastCode(activity, activity.getString(R.string.code_404))
            405 -> showCustomToastCode(activity, activity.getString(R.string.code_405))
            406 -> showCustomToastCode(activity, activity.getString(R.string.code_406))
            407 -> showCustomToastCode(activity, activity.getString(R.string.code_407))
            408 -> showCustomToastCode(activity, activity.getString(R.string.code_408))
            409 -> showCustomToastCode(activity, activity.getString(R.string.code_409))
            410 -> showCustomToastCode(activity, activity.getString(R.string.code_410))
            411 -> showCustomToastCode(activity, activity.getString(R.string.code_411))
            412 -> showCustomToastCode(activity, activity.getString(R.string.code_412))
            413 -> showCustomToastCode(activity, activity.getString(R.string.code_413))
            414 -> showCustomToastCode(activity, activity.getString(R.string.code_414))
            415 -> showCustomToastCode(activity, activity.getString(R.string.code_415))
            416 -> showCustomToastCode(activity, activity.getString(R.string.code_416))
            417 -> showCustomToastCode(activity, activity.getString(R.string.code_417))
            418 -> showCustomToastCode(activity, activity.getString(R.string.code_418))
            422 -> showCustomToastCode(activity, activity.getString(R.string.code_422))
            425 -> showCustomToastCode(activity, activity.getString(R.string.code_425))
            426 -> showCustomToastCode(activity, activity.getString(R.string.code_426))
            428 -> showCustomToastCode(activity, activity.getString(R.string.code_428))
            429 -> showCustomToastCode(activity, activity.getString(R.string.code_429))
            431 -> showCustomToastCode(activity, activity.getString(R.string.code_431))
            451 -> showCustomToastCode(activity, activity.getString(R.string.code_451))
            500 -> showCustomToastCode(activity, activity.getString(R.string.code_500))
            // 501 -> showCustomToastCode(activity, activity.getString(R.string.code_501))
            501 -> {
                invalidJWTTokenAuth(activity)
            }
            502 -> showCustomToastCode(activity, activity.getString(R.string.code_502))
            503 -> showCustomToastCode(activity, activity.getString(R.string.code_503))
            504 -> showCustomToastCode(activity, activity.getString(R.string.code_504))
            505 -> showCustomToastCode(activity, activity.getString(R.string.code_505))
            506 -> showCustomToastCode(activity, activity.getString(R.string.code_506))
            507 -> showCustomToastCode(activity, activity.getString(R.string.code_507))
            508 -> showCustomToastCode(activity, activity.getString(R.string.code_508))
            511 -> showCustomToastCode(activity, activity.getString(R.string.code_511))
        }
    }
    
    HTTP String message 
    
     <string name="code_100">Continue</string>
        <string name="code_101">Switching Protocols</string>
        <string name="code_103">Early Hints</string>
        <string name="code_200">OK</string>
        <string name="code_201">Created</string>
        <string name="code_202">Accepted</string>
        <string name="code_203">Non-Authoritative Information</string>
        <string name="code_204">Data Not Available.!</string>
        <string name="code_205">Reset Content</string>
        <string name="code_206">Partial Content</string>
        <string name="code_300">Multiple Choices</string>
        <string name="code_301">Moved Permanently</string>
        <string name="code_302">Found</string>
        <string name="code_303">See Other</string>
        <string name="code_304">Not Modified</string>
        <string name="code_307">Temporary Redirect</string>
        <string name="code_308">Permanent Redirect</string>
        <string name="code_400">Bad Request</string>
        <string name="code_402">Payment Required</string>
        <string name="code_403">Forbidden</string>
        <string name="code_404">Data Not Available.!</string>
        <string name="code_405">Method Not Allowed</string>
        <string name="code_406">Not Acceptable</string>
        <string name="code_407">Proxy Authentication Required</string>
        <string name="code_408">Request Timeout</string>
        <string name="code_409">already exists.</string>
        <string name="code_410">Gone</string>
        <string name="code_411">Length Required</string>
        <string name="code_412">Precondition Failed</string>
        <string name="code_413">Payload Too Large</string>
        <string name="code_414">URI Too Long</string>
        <string name="code_415">Unsupported Media Type</string>
        <string name="code_416">Range Not Satisfiable</string>
        <string name="code_417">Expectation Failed</string>
        <string name="code_418">I'm a teapot</string>
        <string name="code_422">Unprocessable Entity</string>
        <string name="code_425">Too Early</string>
        <string name="code_426">Upgrade Required</string>
        <string name="code_428">Precondition Required</string>
        <string name="code_429">Too Many Requests</string>
        <string name="code_431">Request Header Fields Too Large</string>
    
        <string name="code_451">Unavailable For Legal Reasons</string>
        <string name="code_500">Fail to connect server.</string>
        <string name="code_501">Not implemented</string>
        <string name="code_502">Bad Gateway</string>
    
        <string name="code_503">Service Unavailable.</string>
    
        <string name="code_504">Gateway Timeout</string>
        <string name="code_505">HTTP Version Not Supported</string>
        <string name="code_506">Variant Also Negotiates</string>
        <string name="code_507">Insufficient Storage</string>
        <string name="code_508">Loop Detected</string>
        <string name="code_511">Network Authentication Required</string>
       
    
    401 Status code Display Popus Unauthorizedmessage
    
    fun unAuthorizedPopUpView(activity: Activity?) {
    
       // Handle your logic for 401 (Unauthorized)
    
    }
    
    fun invalidJWTTokenAuth(activity: Activity?) {
    
        // Handle your logic for 501 (Not implemented)
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      • 2015-10-31
      • 1970-01-01
      • 1970-01-01
      • 2014-02-08
      相关资源
      最近更新 更多