Android app development: HTTP Error

Hi All,

I am trying to create an Android app using Kite Connect 3 API Java Client. I am getting HTTP FAILED: android.os.NetworkOnMainThreadException error while requesting access token using kiteConnect.generateSession(var_requestToken, var_apiSecret). Anyone has any idea how to fix this? Help is greatly appreciated.

Thank you.
  • tonystark
    In Android, you cannot write HTTP calls in your main thread because it will block the UI. So all network requests must be handled in a background thread. Check out this solution.
  • musafirsafwan
    @tonystark .. Thanks for the update. All the examples listed in the link are for getting response for the requests with url and body. But in the case of kiteConnect.generateSession(var_requestToken, var_apiSecret) I don't have access to url and body. Is there anyway to implement an asynchronous task here or do I have to edit KiteRequestHandler? Or is it already implemented?
  • sujith
    It is not recommended to hardcode api_secret inside your app. You need to generate session in server side.
    You can listen to webview events and figure out the final URL with request token. You need to send that request token to the server and that should handle the login and respond to the Android app.
  • musafirsafwan
    @sujith @tonystark

    Thanks for the update.

    I am creating this app for my own personal use. That's why I thought I will store the API secret inside the app since no one else will have access to the app.
    Also, I am not thinking of having a server per se. I thought I will directly use the Kite API to get the data and do the processing inside the android app.
    So for achieving it, I will have to handle network request in a background thread. Is there any way other than editing KiteRequestHandler to solve the problem?
  • sujith
    It is not a good idea, don't edit KiteRequest Handler to handle threading. Your front end or activity should make API calls from a background thread, once you receive the response serve it to UI thread.
  • sujith
    Please create a new thread for new queries, it might help others.
  • musafirsafwan
    @sujith @tonystark

    Thanks for the help.

    I could successfully fetch the data by making API calls from a background thread.

    You can close this thread now.
This discussion has been closed.