Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream was reset: HTTP_1_1_REQUIRED flutter error #3

Open
pravinbhole1 opened this issue Jan 28, 2021 · 2 comments
Open

stream was reset: HTTP_1_1_REQUIRED flutter error #3

pravinbhole1 opened this issue Jan 28, 2021 · 2 comments

Comments

@pravinbhole1
Copy link

while hitting api gives this error

@dishant-livebird
Copy link

package com.brainants.nativehttp.native_http

import android.os.Handler
import android.os.Looper
import androidx.annotation.NonNull
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar
import okhttp3.*
//import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
import java.io.IOException
import java.util.concurrent.TimeUnit

/** NativeHttpPlugin */
class NativeHttpPlugin : FlutterPlugin, MethodCallHandler {
// var client = OkHttpClient()

var client = OkHttpClient.Builder()
    .protocols(okhttp3.internal.Util.immutableList(Protocol.HTTP_1_1))
    .readTimeout(60, TimeUnit.SECONDS)
    .connectTimeout(60, TimeUnit.SECONDS).build()

override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {

    val channel = MethodChannel(flutterPluginBinding.flutterEngine.dartExecutor, "native_http")
    channel.setMethodCallHandler(NativeHttpPlugin())
}

companion object {

    @JvmStatic
    fun registerWith(registrar: Registrar) {
        val channel = MethodChannel(registrar.messenger(), "native_http")
        channel.setMethodCallHandler(NativeHttpPlugin())
    }
}

override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
    if (call.method == "native_http/request") {
        val url = call.argument<String>("url")!!
        val method = call.argument<String>("method")!!
        var headers : MutableMap<Any?,Any?> = call.argument<MutableMap<Any?, Any?>>("headers")!!
        var body :MutableMap<Any?,Any?> = call.argument<MutableMap<Any?, Any?>>("body")!!
        if (headers == null) headers = HashMap()
        if (body == null) body = HashMap()
        sendRequest(url, method, headers, body, result)
    } else {
        result.notImplemented()
    }
}

// val JSON: MediaType = "application/json; charset=utf-8".toMediaType()
val JSON = MediaType.get("application/json; charset=utf-8")
fun sendRequest(url: String, method: String, headers: MutableMap<Any?,Any?>, body: MutableMap<Any?, Any?>, @nonnull result: Result) {
// val requestBody: RequestBody = JSONObject(body).toString().toRequestBody(JSON)
val requestBody: RequestBody = RequestBody.create(JSON,JSONObject(body).toString())
var requestBuilder: Request.Builder = Request.Builder()
.url(url)
headers.entries.forEach {
requestBuilder = requestBuilder.addHeader(it.key as String, it.value as String)
}

    if (method != "GET")
        requestBuilder = requestBuilder.method(method, requestBody)

    val request = requestBuilder.build()
    val mHandler = Handler(Looper.getMainLooper())
    client.newCall(request).enqueue(
            object : Callback {

                override fun onFailure(call: Call, e: IOException) {
                    mHandler.post {
                        result.error(e.message, e.localizedMessage, null)
                    }
                }

                override fun onResponse(call: Call, r: Response) {
                    val response = HashMap<String, Any>()
                    response["code"] = r.code()
                    response["body"] = r.body()!!.string()
                    mHandler.post {
                        result.success(response)
                    }
                }
            }
    )
}


override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
}

}

@dishant-livebird
Copy link

package com.brainants.nativehttp.native_http

import android.os.Handler import android.os.Looper import androidx.annotation.NonNull import io.flutter.embedding.engine.plugins.FlutterPlugin import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.MethodChannel.MethodCallHandler import io.flutter.plugin.common.MethodChannel.Result import io.flutter.plugin.common.PluginRegistry.Registrar import okhttp3.* //import okhttp3.RequestBody.Companion.toRequestBody import org.json.JSONObject import java.io.IOException import java.util.concurrent.TimeUnit

/** NativeHttpPlugin */ class NativeHttpPlugin : FlutterPlugin, MethodCallHandler { // var client = OkHttpClient()

var client = OkHttpClient.Builder()
    .protocols(okhttp3.internal.Util.immutableList(Protocol.HTTP_1_1))
    .readTimeout(60, TimeUnit.SECONDS)
    .connectTimeout(60, TimeUnit.SECONDS).build()

override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {

    val channel = MethodChannel(flutterPluginBinding.flutterEngine.dartExecutor, "native_http")
    channel.setMethodCallHandler(NativeHttpPlugin())
}

companion object {

    @JvmStatic
    fun registerWith(registrar: Registrar) {
        val channel = MethodChannel(registrar.messenger(), "native_http")
        channel.setMethodCallHandler(NativeHttpPlugin())
    }
}

override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
    if (call.method == "native_http/request") {
        val url = call.argument<String>("url")!!
        val method = call.argument<String>("method")!!
        var headers : MutableMap<Any?,Any?> = call.argument<MutableMap<Any?, Any?>>("headers")!!
        var body :MutableMap<Any?,Any?> = call.argument<MutableMap<Any?, Any?>>("body")!!
        if (headers == null) headers = HashMap()
        if (body == null) body = HashMap()
        sendRequest(url, method, headers, body, result)
    } else {
        result.notImplemented()
    }
}

// val JSON: MediaType = "application/json; charset=utf-8".toMediaType() val JSON = MediaType.get("application/json; charset=utf-8") fun sendRequest(url: String, method: String, headers: MutableMap<Any?,Any?>, body: MutableMap<Any?, Any?>, @nonnull result: Result) { // val requestBody: RequestBody = JSONObject(body).toString().toRequestBody(JSON) val requestBody: RequestBody = RequestBody.create(JSON,JSONObject(body).toString()) var requestBuilder: Request.Builder = Request.Builder() .url(url) headers.entries.forEach { requestBuilder = requestBuilder.addHeader(it.key as String, it.value as String) }

    if (method != "GET")
        requestBuilder = requestBuilder.method(method, requestBody)

    val request = requestBuilder.build()
    val mHandler = Handler(Looper.getMainLooper())
    client.newCall(request).enqueue(
            object : Callback {

                override fun onFailure(call: Call, e: IOException) {
                    mHandler.post {
                        result.error(e.message, e.localizedMessage, null)
                    }
                }

                override fun onResponse(call: Call, r: Response) {
                    val response = HashMap<String, Any>()
                    response["code"] = r.code()
                    response["body"] = r.body()!!.string()
                    mHandler.post {
                        result.success(response)
                    }
                }
            }
    )
}


override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
}

}

add package to your local flutter project and
change code in native android

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants