Testgpt
Testgpt
>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:padding="20dp"
android:orientation="vertical"
android:background="#0E193D"
android:gravity="bottom">
<TextView
android:id="@+id/txt_response"
android:layout_width="match_parent"
android:layout_height="250dp"
android:textStyle="bold"
android:textColor="@color/white"
android:textSize="20sp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="20dp"
/>
<TextView
android:id="@+id/txt_my_response"
android:layout_width="match_parent"
android:layout_height="250dp"
android:textStyle="bold|italic"
android:textColor="#FFC107"
android:textSize="20sp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<EditText
android:id="@+id/edt_question"
android:layout_width="330dp"
android:layout_height="wrap_content"
android:hint="Enter your question here !"
android:textColorHint="@color/white"
android:textColor="@color/white"
android:textSize="20dp"
android:textStyle="bold"
/>
<ImageView
android:id="@+id/btn_send"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_send"
/>
</LinearLayout>
</LinearLayout>
package pro.developer.chatgpt
import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONArray
import org.json.JSONObject
import java.io.IOException
@SuppressLint("CutPasteId", "WrongViewCast")
private val client = OkHttpClient()
@SuppressLint("WrongViewCast")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn_send.setOnClickListener {
val question = edt_question.text.toString()
txt_myresp.setText("$question")
getResponse(question) { response ->
runOnUiThread {
txt_resp.text = response
}
}
}
}
.post(requestBody.toRequestBody("application/json".toMediaTypeOrNull()))
.build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
Log.e("error","API failed",e)
}