如何从onResponse返回值

时间:2018-02-12 03:50:32

标签: java android retrofit

基本上这是代码结构,我想知道如何修改我的代码,以便我可以在onResponse中获取值并返回它。截至目前,我的mainReply变量返回"(空白)"但是我希望它能在我的onResponse段中传递名为细节的arraylist中的数据。请放心,我已经检查了返回的值,但我无法将值传递出onResponse段。

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

Joke

2 个答案:

答案 0 :(得分:1)

如果你想修改你现有的代码,你可以添加一个类似于我加入的界面(RevealDetailsCallbacks),将其传递给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();
            }
        });
    }
}

答案 1 :(得分:0)

您可以立即在OnClick中将Retrofit呼叫排入队列,并在那里处理其响应

  return this.esriLoader.load({  
        url: 'https://js.arcgis.com/4.6/' 
    }).then(() => {
        return this.esriLoader.loadModules([
            'esri/Map', 'esri/views/MapView', 'esri/layers/GraphicsLayer', 'esri/layers/FeatureLayer', "esri/tasks/Geoprocessor",
            "esri/tasks/support/FeatureSet", "esri/Graphic","esri/geometry/Point"
        ]).then(([
            Map, MapView, GraphicsLayer, FeatureLayer, Geoprocessor, FeatureSet, Graphic,PointObj
        ]) => {

       //this is where the object comes from that allows me to say 
       //new this._mapDependencies.FeatureLayer() or Point() etc
       //once again, new Point() works, but FeatureLayer() does not, 
       //even though both objects are filled

            return {
                Geoprocessor: Geoprocessor,                  
                Graphic: Graphic,
                GraphicsLayer: GraphicsLayer,
                FeatureLayer: FeatureLayer,
                FeatureSet: FeatureSet,
                Point:PointObj
            }
        });
    });