如何将数据从okhttp异步线程回发到主线程中的ArrayAdapter?

时间:2017-06-01 23:23:48

标签: android listview okhttp

我的目的是通过okhttp接口获取从服务器获取的数据。 okhttp方法在异步中运行,因此在与活动不同的线程上运行。有问题的活动是一个创建listView的片段。我试图通过ArrayAdapter用okhttp中的数据填充listView。

如何将数据从okhttp异步线程回发到ArrayAdapter?

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {



    View view = inflater.inflate(R.layout.fragment_friends, container, false);
    final ListView listview = (ListView) view.findViewById(R.id.friendListView);

    ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, nfl);
    listview.setAdapter(listViewAdapter);


    OkHttpClient client = new OkHttpClient();
    String url = "http://192.168.8.101:7777/friendlist";
    Request request = new Request.Builder()
            .url(url)
            .build();
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }
            String RD = response.body().string();
            String trimmedList = RD.substring(1, RD.length()-1);
            String[] fl = trimmedList.split(",");
            final List<String> nfl = new ArrayList<String>();
            for (String element : fl){
                String ee = element.replaceAll("^\"|\"$", "");
                nfl.add(ee);
            }


        }
    });


    return view;
}

3 个答案:

答案 0 :(得分:1)

如果你有对活动实例的引用,你可以创建一个runnable来填充适配器并使用活动的runOnUiThread方法运行它

签出文档:

https://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)

答案 1 :(得分:0)

您必须启动适配器并在onResponse CallBack中列出。如果没有响应,只显示错误对话框,否则,使用结果启动适配器。代码必须如下所示:

@Override
        public void onResponse(Call call, Response response) throws IOException {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }
            String RD = response.body().string();
            String trimmedList = RD.substring(1, RD.length()-1);
            String[] fl = trimmedList.split(",");
            final List<String> nfl = new ArrayList<String>();
            for (String element : fl){
                String ee = element.replaceAll("^\"|\"$", "");
                nfl.add(ee);
            }
//Put your code for showing the list item here.
ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, nfl);
    listview.setAdapter(listViewAdapter);


        }

答案 2 :(得分:0)

答案是创建runOnUiThread并在runnable()

中调用ArrayAdapter

这是正确的代码:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_friends, container, false);
    final ListView listview = (ListView) view.findViewById(R.id.friendListView);


    OkHttpClient client = new OkHttpClient();
    String url = "http://192.168.8.101:7777/friendlist/" + USERNAME;
    Request request = new Request.Builder()
            .url(url)
            .build();
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }
            String RD = response.body().string();
            response.close();
            String trimmedList = RD.substring(1, RD.length()-1);
            String[] fl = trimmedList.split(",");
            final List<String> nfl = new ArrayList<String>();
            for (String element : fl){
                String ee = element.replaceAll("^\"|\"$", "");
                nfl.add(ee);
            }

            System.out.println(nfl);
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, nfl);
                    listview.setAdapter(listViewAdapter);
                }
            });

        }
    });


    return view;
}
相关问题