我的线程有问题

时间:2019-02-23 11:27:25

标签: java android multithreading

在此之前,我隐藏了字段并显示了新字段。所有这些都在1个活动中发生。现在,我决定将它们分为2个活动。这样当您按下按钮时立即移到主线程。现在,我有了“只有创建视图层次结构的原始线程才能触摸其视图”的信息。我还要添加Handler。

public class Main3Activity extends AppCompatActivity {
    Button button1;
    TextView tv;
    ListView listView1;
    private static String TAG_SCHEDULE = "raspisanie";
    private static String TAG_ID = "id";
    private static String TAG_PREDM = "urok";
    private static String TAG_TIME = "id_day";


    ArrayList<HashMap<String, String>> scheduleList;


    private Handler handler;

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

        scheduleList = new ArrayList<HashMap<String, String>>();

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {

                ListAdapter adapter = new SimpleAdapter(Main3Activity.this, scheduleList, R.layout.activity_column, new String[]{TAG_TIME, TAG_PREDM}, new int[]{R.id.Coldayid, R.id.ColUrok});

                ListView lv = (ListView) findViewById(R.id.listView1);
                lv.setAdapter(adapter);
            }
        };
    }

    public String url1 = "https://domex666.000webhostapp.com/rasp4.php?id_group=";
    public void tester(View view) {
        updateHTTP();
    }
    public String getURL() {
         button1 = (Button) findViewById(R.id.button1);
        Intent intent = getIntent();
        tv = (TextView) findViewById(R.id.tv);
        String grp = intent.getStringExtra("grp");
        String uri = "";
        tv.setText(grp);
        String id_group = tv.getText().toString();
        uri = url1 + id_group;
        Log.d("URL Request >>",uri);
        return uri;
    }
    protected void updateHTTP() {
        scheduleList.clear();
        Log.d("GO GO >>","START THIS PROGRAMM");

        Thread thread = new Thread(new Runnable(){
            @Override
            public void run() {
                try{
                    String newURL = getURL();
                    URL url = new URL(newURL);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setReadTimeout(10000);
                    conn.setConnectTimeout(15000);
                    conn.setRequestMethod("GET");
                    conn.setDoInput(true);
                    conn.connect();
                    InputStream stream = conn.getInputStream();
                    String data = convertStreamToString(stream);
                    Log.d("DATA >> ",data);
                    JSONObject dataJsonObj = new JSONObject(data);
                    JSONArray products = dataJsonObj.getJSONArray(TAG_SCHEDULE);

                    for(int i = 0; i < products.length();i++) {
                        JSONObject schedule = products.getJSONObject(i);
                        String id_sched = schedule.getString(TAG_ID);
                        String predm = schedule.getString(TAG_PREDM);
                        String time = schedule.getString(TAG_TIME);
                        HashMap<String, String> sched = new HashMap<String, String>();
                        sched.put(TAG_ID, id_sched);
                        sched.put(TAG_PREDM, predm);
                        sched.put(TAG_TIME,time);
                        scheduleList.add(sched);
                    }

                    Message msg = new Message();
                    msg.obj = scheduleList;
                    handler.sendMessage(msg);
                    Log.d("Request >>>>","SUCCESS");
                } catch (Exception e) {
                    Log.d("ERROR ctaciv>",e.getMessage());
                }
            }

        });

        thread.start();
    }


    static String convertStreamToString(java.io.InputStream is) {
        java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }

}

0 个答案:

没有答案