gpt4 book ai didi

kotlin - 无法在 kotlin 中将 AsyncTask 转换为 Retrofit2

转载 作者:行者123 更新时间:2023-12-02 09:27:50 24 4
gpt4 key购买 nike

我正在尝试将以下代码(java)转换为在kotlin中使用retrofit2发布请求,但是,我发现retrofit2文档很难理解。我尝试使用@POST 并发送请求但失败。如果您能给我一些提示或示例,那将是一个很大的帮助。我很乐意听到像您这样的专业人士的意见!

这是我要发送的请求类型

curl -X POST https://api.a3rt.recruit-tech.co.jp/talk/v1/smalltalk \
-F "apikey=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" \
-F "query=test"

以下是我要转换为retrofit2的实际代码

public class TalkAysncTask extends AsyncTask<String, String, String> {

private static final String API_KEY = YOUR API KEY;
private static final String API_URL =
"https://api.a3rt.recruit-tech.co.jp/talk/v1/smalltalk";

interface Callback{
void onReturn(String s);
}

private Callback callback;

TalkAysncTask(Context context){
callback = (Callback) context;
}

@Override
protected String doInBackground(String... params) {

RequestBody body = new FormBody.Builder()
.add("apikey", API_KEY)
.add("query", params[0])
.build();

Request request = new Request.Builder()
.url(API_URL)
.post(body)
.build();

OkHttpClient client = new OkHttpClient.Builder()
.build();

try{
Response response = client.newCall(request).execute();

return response.body().string();
}catch (IOException e){
e.printStackTrace();
}

return null;
}

@Override
protected void onPostExecute(String s) {
Log.d("debug", s);
callback.onReturn(s);
}
}

我也想知道如何获得以下类型的响应

{
status: 200,
message: "ok",
results: [{
perplexity: 2.3688167429546714,
reply: "tested"
}],
}

我尝试使用以下代码但没有成功

interface AiClient {
@FormUrlEncoded
@POST("/talk/v1/smalltalk")
fun request(@Field("apikey") apiKey: String,
@Field("query") query: String?): Call<ApiResponse>

companion object {
fun create(): AiClient {
val retrofit = retrofit2.Retrofit.Builder()
.baseUrl("https://api.a3rt.recruit-tech.co.jp")
.addConverterFactory(GsonConverterFactory.create())
.build()
return retrofit.create(AiClient::class.java)
}
}
}

最佳答案

创建模型类:

class ApiResponse(var status:Int, var message:String, var results:MutableList<Results>){
class Results(var perplexity:Double,var reply:String)
}

更改您的 AiClient:

interface  AiClient {
@FormUrlEncoded
@POST("talk/v1/smalltalk")
fun request(@Field("apikey") apiKey: String,
@Field("query") query: String?): retrofit2.Call<ApiResponse>

companion object {
fun create(): AiClient {
val retrofit = retrofit2.Retrofit.Builder()
.baseUrl("https://api.a3rt.recruit-tech.co.jp/")
.addConverterFactory(GsonConverterFactory.create())
.build()
return retrofit.create(AiClient::class.java)
}
}
}

调用 Api:

AiClient.create().request("yourkey","yourquery").enqueue(object :Callback<ApiResponse> {
override fun onFailure(call: Call<ApiResponse>?, t: Throwable?) {
}

override fun onResponse(call: Call<ApiResponse>, response: Response<ApiResponse>) {
if (response.isSuccessful){
var result=response.body()
if (result!=null){
result.message
}


}

}
})
}

关于kotlin - 无法在 kotlin 中将 AsyncTask 转换为 Retrofit2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49012731/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com