gpt4 book ai didi

java - 如何从 onResponse 返回值

转载 作者:行者123 更新时间:2023-12-02 11:38:36 28 4
gpt4 key购买 nike

基本上这是代码结构,我想知道如何修改我的代码,以便我可以获取 onResponse 内的值并返回它。截至目前,我的 mainReply 变量返回“(空白)”,但我希望它传递数组列表中的数据,称为 onResponse 段中的详细信息。请放心,我已经检查过返回了值,但我只是无法获取要从 onResponse 段传递出去的值。

我已经检查了替代方案,他们提到使用界面。但是,我不知道如何修改我的代码以使用提到接口(interface)和回调使用的解决方案。

public class MainActivity extends AppCompatActivity {

EditText et_message;
FloatingActionButton fab_send;
API api;
ListView list_view_conversation;
List<ChatModel> list_chat = new ArrayList<>();
RevealDetailsCallbacks callback;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

et_message = (EditText) findViewById(R.id.et_message);
fab_send = (FloatingActionButton) findViewById(R.id.fab_send);
list_view_conversation = (ListView) findViewById(R.id.list_view_conversation);

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();

api = retrofit.create(API.class);

fab_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//this method ultimately is to get response and send back to user
String s = et_message.getText().toString();
ChatModel model = new ChatModel(s, true);
list_chat.add(model);
new retrieveDetails().execute(list_chat);

et_message.setText("'");
}
});

}

public class retrieveDetails extends AsyncTask<List<ChatModel>, Void, String> {
String text = et_message.getText().toString();
String mainReply = "";
List<ChatModel> models;
List<String> details = new ArrayList<String>();

@Override
public String doInBackground(List<ChatModel>[] lists) {
Call<List<Patient>> call = api.getPatients();
models = lists[0];
call.enqueue(new Callback<List<Patient>>() {
public String reply;

@Override
public void onResponse(Call<List<Patient>> call, Response<List<Patient>> response) {
List<Patient> patients = response.body();

for (int i = 0; i < patients.size(); i++) {
if (patients.get(i).getNric().equals(text)) {
details.add("Name: " + patients.get(i).getName() + "\nNRIC: " + patients.get(i).getNric()
+ "\nDOB: " + patients.get(i).getDob() + "\nContact No: " + patients.get(i).getContactno());
}
}
this.mainReply = details.get(0);
Log.i("Here Log i", reply);
}

@Override
public void onFailure(Call<List<Patient>> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
return mainReply;//I want to reply with the data added into the details arraylist in the onResponse segment
}


@Override
public void onPostExecute(String s) {
ChatModel chatModel = new ChatModel(s, false);
models.add(chatModel);
CustomAdapter adapter = new CustomAdapter(models, getApplicationContext());
list_view_conversation.setAdapter(adapter);
}
}


}

最佳答案

如果您想修改现有代码,您可以添加一个像我在顶部添加的接口(interface) (RevealDetailsCallbacks) 一样的接口(interface),将其传递到 asynctask 构造函数中,然后运行它。代码如下所示:

public class MainActivity extends AppCompatActivity {

//Interface callback here
interface RevealDetailsCallbacks {
public void getDataFromResult(List<String> details);
}

EditText et_message;
FloatingActionButton fab_send;
API api;
ListView list_view_conversation;
List<ChatModel> list_chat = new ArrayList<>();
RevealDetailsCallbacks callback;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

et_message = (EditText) findViewById(R.id.et_message);
fab_send = (FloatingActionButton) findViewById(R.id.fab_send);
list_view_conversation = (ListView) findViewById(R.id.list_view_conversation);

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();

this.callback = new RevealDetailsCallbacks() {
@Override
public void getDataFromResult(List<String> details) {
//Do stuff here with the returned list of Strings
}
};

api = retrofit.create(API.class);

fab_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//this method ultimately is to get response and send back to user
String s = et_message.getText().toString();
ChatModel model = new ChatModel(s, true);
list_chat.add(model);
new retrieveDetails(callback).execute(list_chat);

et_message.setText("'");
}
});

}

public class retrieveDetails extends AsyncTask<List<ChatModel>, Void, String> {
String text = et_message.getText().toString();
String mainReply = "";
List<ChatModel> models;
List<String> details = new ArrayList<String>();
private RevealDetailsCallbacks listener;

retrieveDetails(RevealDetailsCallbacks listener){
this.listener = listener;
}

@Override
public String doInBackground(final List<ChatModel>[] lists) {
Call<List<Patient>> call = api.getPatients();
models = lists[0];
call.enqueue(new Callback<List<Patient>>() {
public String reply;

@Override
public void onResponse(Call<List<Patient>> call, Response<List<Patient>> response) {
List<Patient> patients = response.body();

for (int i = 0; i < patients.size(); i++) {
if (patients.get(i).getNric().equals(text)) {
details.add("Name: " + patients.get(i).getName() + "\nNRIC: " + patients.get(i).getNric()
+ "\nDOB: " + patients.get(i).getDob() + "\nContact No: " + patients.get(i).getContactno());
}
}
this.mainReply = details.get(0);
Log.i("Here Log i", reply);
if(listener != null) {
listener.getDataFromResult(details);
}
}

@Override
public void onFailure(Call<List<Patient>> call, Throwable t) {
//Don't make a toast here, it will throw an exception due to it being in doInBackground
//Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
return mainReply;//I want to reply with the data added into the details arraylist in the onResponse segment
}


@Override
public void onPostExecute(String s) {
ChatModel chatModel = new ChatModel(s, false);
models.add(chatModel);
CustomAdapter adapter = new CustomAdapter(models, getApplicationContext());
list_view_conversation.setAdapter(adapter);
}
}
}

但是,这里不需要 asynctask,因为您正在运行 Retrofit 并调用 .enqueue,它在后台线程上运行。更简单的版本如下所示:

public class MainActivity extends AppCompatActivity {

//Interface callback here
interface RevealDetailsCallbacks {
public void getDataFromResult(List<String> details);
}

//Keep your same variables here

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Same setup here

this.callback = new RevealDetailsCallbacks() {
@Override
public void getDataFromResult(List<String> details) {
//Do stuff here with the returned list of Strings
}
};


fab_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Same setup here, then call the method
makeWebCalls();
}
});

}

private void makeWebCalls(){
Call<List<Patient>> call = api.getPatients();
models = lists[0];
call.enqueue(new Callback<List<Patient>>() {
@Override
public void onResponse(Call<List<Patient>> call, Response<List<Patient>> response) {
//Run your response code here. When done, pass to the callback
}

@Override
public void onFailure(Call<List<Patient>> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}

关于java - 如何从 onResponse 返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48739284/

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