Open pravinbhole1 opened 3 years ago
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) {
}
}
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
while hitting api gives this error