1

This is the response i am trying to fetch from the api

{
    "success": {
        "id": 35,
        "otp": 9830,
        "msg": "Otp has sent on this number"
    }
}

The response is working absolutely fine when checked in postman . However when i try to fetch the same json by parsing the response in Hashmap format in android kotlin i get the E/Error: retrofit2.adapter.rxjava2.HttpException: HTTP 401 Unauthorized

Now This is My android rxjava kotlin code as below where i am passing all necessery parameters but the response goes to error block. The code is as follows

override fun login(api_token: String, name: String, mobile: String, lat: String, lon: String, address: String) {

      var subscribe = api.login(api_token,mobile.toInt(),lat.toDouble(),lon.toDouble(),address).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ list: LoginParentResponseModel? ->
                    Log.i("login",list!!.toString())
                    view.showProgress(false)
                    view.loadLoginSuccess(list!!.success)
                }, { error ->
                    view.loadLoginFailure("loginfailed")
                    view.showProgress(false)
                    view.showErrorMessage("problen"+error.toString())


                })
        subscriptions.add(subscribe)


}

Now this is the Model class for reponse handling

data class LoginParentResponseModel(val success : Map<String,String>,val error : Map<String,String>)

Now this is the api and its parameters which is fine as like i am using in postman

@FormUrlEncoded
    @POST("api/register?")
    fun login(@Field("api_token") id: String, @Field("mobile") mobile: Int,@Field("lat") lat: Double,@Field("lon") lon: Double,@Field("address") address: String): Observable<LoginParentResponseModel>

Please help me to resolve this issue. Your help would be much appreciated. Thanks in advance

Buraq24 Rep
  • 23
  • 1
  • 9
  • 2
    401 is the return code from the server. Its saying you aren't authorized, or that authentication failed. You need to debug on the server what it got and why it sent back a 401. – Gabe Sechan Sep 05 '18 at 07:04
  • Maybe this question could help you: https://stackoverflow.com/questions/42375566/getting-401-unauthorised-in-retrofit-but-works-in-postman-and-swift – Juanjo Berenguer Sep 05 '18 at 07:06
  • but how can i even check error body – Buraq24 Rep Sep 05 '18 at 08:37
  • any idea please – Buraq24 Rep Sep 05 '18 at 08:37
  • 1
    You should change response type to `Response>` then you can see error body – sma6871 Sep 05 '18 at 12:22
  • Add request logging in your retrofit(https://stackoverflow.com/a/33328524/1320616). That way you will be able to view request(request body, url, headers etc.) as well as the response. Once you are able to view the request parameters, use any rest client like Postman and try to hit the server with the same parameters. – Ankit Aggarwal Sep 06 '18 at 13:34

0 Answers0