运行您的请求并接收新数据Json

时间:2016-09-27 13:45:58

标签: java android

我有问题。当我在流中进行查询时,我已经从URL下载了数据,一切正常。但是当我通过按doInBackground()方法调用AsynsTask示例时返回相同的数据,但它们会在URL上更新。只要程序重新启动,它们就不会更新。

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

            URL url = new URL("data.php?"+new Random().nextInt(200));

            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();

            InputStream inputStream = urlConnection.getInputStream();
            StringBuffer buffer = new StringBuffer();

            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

            String line;
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }

            jSON_R = buffer.toString();


        } catch (Exception e) {
            e.printStackTrace();
        }

        return jSON_R;
    }
  

所有代码

public class ParseTask extends AsyncTask<Void, Void, String> {

    int intRow = 0;
    String jSON_R = "";

    private List<User> movieList;


    Activity act;

    ListView list;
    LAdapter adapter;

    boolean Unique = true;

    public ParseTask (Activity act){
        this.act = act;
    }


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

                URL url = new URL("data.php?"+new Random().nextInt(200));

                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();

                InputStream inputStream = urlConnection.getInputStream();
                StringBuffer buffer = new StringBuffer();

                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

                String line;
                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                }

                jSON_R = buffer.toString();


            } catch (Exception e) {
                e.printStackTrace();
            }

            return jSON_R;
        }

    @Override
    protected void onPostExecute(String strJson) {
        super.onPostExecute(strJson);

        list = (ListView) act.findViewById(R.id.listVew);

        Button b = (Button) act.findViewById(R.id.refresh);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //To do
            }
        });


        movieList = new ArrayList<>();

        adapter = new LAdapter(act, movieList);
        list.setAdapter(adapter);

        try {
            JSONObject dataJsonObj = new JSONObject(strJson);
            JSONArray jsa = dataJsonObj.getJSONArray("data");

            for (int i = 0; i < jsa.length(); i++) {
                JSONObject data1 = chat.getJSONObject(i);
                String mes = data1.getString("mes1");
                String mes2 = data1.getString("mes2");
                String mes3 = data1.getString("mes3");

                User m = new User(mes, mes2, mes3);

                movieList.add(0, m);
            }

            adapter.notifyDataSetChanged();

            intRow = jsa.length();


        } catch (JSONException e) {
            e.printStackTrace();
        }




        list.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {}

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

                if (firstVisibleItem > 1){
                    Unique = false;
                }else{
                    Unique = true;
                }

            }
        });

        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    while (true){
                        sleep(5000);
                        if (Unique){
                            act.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Update();
                                }
                            });
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        thread.start();
    }

    private void Update(){

            try {
            JSONObject dataJsonObj = new JSONObject(strJson);
            JSONArray jsa = dataJsonObj.getJSONArray("data");

            for (int i = 0; i < jsa.length(); i++) {
                JSONObject data1 = chat.getJSONObject(i);
                String mes = data1.getString("mes1");
                String mes2 = data1.getString("mes2");
                String mes3 = data1.getString("mes3");

                User m = new User(mes, mes2, mes3);

                movieList.add(0, m);
            }

            adapter.notifyDataSetChanged();

            intRow = jsa.length();


        } catch (JSONException e) {
            e.printStackTrace();
        }

            adapter.notifyDataSetChanged();

        }

}
  

以这种方式调用对象

new ParseTask(getActivity()).execute();

1 个答案:

答案 0 :(得分:0)

关闭连接。

我的意思是使用(更好)reader.close()inputStream.close()

或者尝试使用jsoup库https://dotnetfiddle.net/y1a30V