0

I have a little problem. In my android app, i have to connect to an online server. The connection should be called with an click-button an as an backround service. I know that I must put the network connection in a thread, but i don't know how it works. I put the call in a own method wich creates a thread but at compilation i got a error message.

here is my code:

Button:

 ImageView refresher = (ImageView)findViewById(R.id.imgRefresh);
    refresher.setOnClickListener(new OnClickListener(){
    @Override
            public void onClick(View arg0) {                                        
                    httpThread();                           
                    }                       
                });   

thread method:

private void httpThread(){
    final Handler h = new Handler();
    Thread thread = new Thread(new Runnable(){
        @Override
        public void run() {
            h.post(new Runnable(){

                @Override
                public void run() {
                    try{                            
                        vomServerholenUndSpeichern();
                        FileInputStream inStream = null;
                        try {
                            inStream = openFileInput("test.xml");
                        } catch (FileNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        try {
                            leseDatei(inStream);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        createListView("test.xml");
                        System.out.println(inStream);
                        drawListView();
                    } catch (Exception e){
                        e.printStackTrace();
                    }                           
                }

            });

        }       
    });
    thread.start();     
}

Can someone help me to fix the problem?

  • what is the error message? and go read about AsyncTask. – Olayinka Jun 23 '14 at 10:17
  • you can try to solve this with this example http://stackoverflow.com/a/9671602/1726419 – yossico Jun 23 '14 at 10:22
  • Please post the logcat with the error(s) and post the code for `vomServerholenUndSpeichern()`. And `drawListView()`should be done NOT in a thread. You did not say that there was a `leseDatei` and what it should do there. How should we know? – greenapps Jun 23 '14 at 11:28

1 Answers1

0

You have to use AsyncTask, is not difficult.

AsyncTask are necessary to avoid block UI during your connection operation, the method doInbackGround of AsyncTask is used for this kind of operations (it runs in a thread different from the UI), while int the onPostExecute (this run on the UI) of the task you will manage your httpResult;)

axl coder
  • 739
  • 3
  • 19
  • No. He does not have to use an AsyncTask. Moreover he already uses a Thread. An AsyncTask would make it easier. That's true. – greenapps Jun 23 '14 at 11:27