在OKHttp之后更新MainActivity变量

时间:2018-03-10 15:59:03

标签: android list okhttp

我正在使用OKHttp从服务器提取数据,并在按钮点击后查看更新MainActivity中的List对象变量。在首次启动应用程序(而不是一次点击)期间,代码需要两次按钮才能更新列表。如何在一次单击(它应该工作的方式)后获取更新的列表。请注意,我尝试了不同的方案,例如使List对象静态并在线程中更新并在线程完成之后。我的压缩代码如下。

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getName();
    Button button;
    public TextView jsonTextView;
    String url =   "http://www.mocky.io/v2/5aa0d0c8320000941ce9fd7c";
    private static List<ClientsInfo> infoData; //static variable to be 
                                           //updated after threading 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       button = findViewById(R.id.button);
       infoData = new ArrayList();
       button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getJsonDataAndProcess();
            }
        });
    }

    private void getJsonDataAndProcess() {
        OkHttpClient client = new OkHttpClient();
        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, final Response response) 
                        throws IOException {
                if (response.isSuccessful()) {
                    final String myResponse = response.body().string();
                    MainActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                             formatJsonData(myResponse);
                        }
                    });
                }
            }
        });
    }

    public void formatJsonData(String response){
        try{
            //Json object of the parsed data
            JSONArray JA = new JSONArray(response);
            if( infoData != null)
                infoData.clear();

            for (int i = 0; i < JA.length(); i++){

                JSONObject JO = (JSONObject) JA.get(i);
                ClientsInfo info = new ClientsInfo();//object stored in list
                info.setId_Number( Integer.toString((Integer) JO.get("id")));
                info.setFirst_Name((String) JO.get("first_name"));
                info.setLast_Name((String) JO.get("last_name"));
                info.setEmail((String) JO.get("email"));
                info.setGender((String) JO.get("gender"));
                info.setIp_Address((String) JO.get("ip_address"));
                infoData.add(info);
            }
        } catch(Exception e){
            Log.i("ERROR2", e.toString());
        }
    }
}
//end of mainActivity

1 个答案:

答案 0 :(得分:0)

填写完信息列表后,您只需在notifyDataSetChanged功能中添加formatJsonData即可。

public void formatJsonData(String response){
    try{
        //Json object of the parsed data
        JSONArray JA = new JSONArray(response);
        if(infoData != null)
            infoData.clear();

        for (int i = 0; i < JA.length(); i++){
            JSONObject JO = (JSONObject) JA.get(i);
            ClientsInfo info = new ClientsInfo();//object stored in list
            info.setId_Number( Integer.toString((Integer) JO.get("id")));
            info.setFirst_Name((String) JO.get("first_name"));
            info.setLast_Name((String) JO.get("last_name"));
            info.setEmail((String) JO.get("email"));
            info.setGender((String) JO.get("gender"));
            info.setIp_Address((String) JO.get("ip_address"));
            infoData.add(info);
        }

        // Just call the notifyDataSetChanged here on your adapter.
        yourAdapter.notifyDataSetChanged();

    } catch(Exception e){
        Log.i("ERROR2", e.toString());
    }
}