'nanoHttpD nano server handling two parallels request , each request needs to work independently

I have hosted my nano server in my local host and inside the serve method, I am performing external intent as below.

class MyServer(context2: Context) : NanoHTTPD(Companion.PORT) {
        var context: Context
        override fun serve(session: IHTTPSession): Response {
                 val uriValue = session.uri
                 val callIntent = Intent("android.intent.action.GET_CONTENT", 
                 Uri.parse("rguestpayandroid://localhost:8080$uriValue"))
                 callIntent.addCategory("android.intent.category.DEFAULT")
                 callIntent.putExtra("RequestData", files["postData"])
                 (context as PostmanActivity).startActivityForResult(callIntent,firstrequestID)
       }
 }

Based on the uri, I am finding whether it's the first or second request. Based on the request, corresponding intent has been triggered and request-id has been set.

Globally I have declared two variables for the response.

var firstResponse: String? = null var secondResponse: String? = null

On my activityResult override method, Based on the request code, I am fetching the response and reply in nanohttp request.

  public override fun onActivityResult(requestCode: Int, resultCode: Int, response: Intent?) {
    
    super.onActivityResult(requestCode, resultCode, response)
    when (requestCode) {
      "firstrequestid" -> firstResponse = response.getStringExtra(IntentConstants.ResponseData)
      "secondrequestid" -> secondResponse = response.getStringExtra(IntentConstants.ResponseData)
 }

When the first request is in progress, if we trigger the second request means, the second request-response has been delayed till the first request-response is obtained.

How to fix this issue.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source