How to use separate thread to perform http requests

前端 未结 2 1868
有刺的猬
有刺的猬 2020-12-02 17:40

I have an application that is performing HTTP Requests (specifically calling the FogBugz API) when the user clicks certain buttons. Right now, I am just creating a service w

相关标签:
2条回答
  • 2020-12-02 17:55

    The simplest way to do it would just be to do something like

    //Body of your click handler
    Thread thread = new Thread(new Runnable(){
      @Override
      public void run(){
        //code to do the HTTP request
      }
    });
    thread.start();
    

    That will cause the code inside the run() method to execute in a new thread of execution. You can look into an async task if you like although I've personally never used it. This is a quick and simple way to get things done.

    With regards to passing information back, I would use a Handler object which effectively allows you to set up a message queue for a given thread and pass messages to it which cause the execution of specific code. The reason you need to do this is because Android will not let any thread other than the UI thread update the actual UI.

    Does that address your question? I know my first pass didn't fully address all of your issues.

    Edit Basically, what you do is define a handler object in your Activity like

    private Handler handler_ = new Handler(){
        @Override
        public void handleMessage(Message msg){
    
        }
    
    };
    

    You also create static int constants that help tell the handler what to do. You basically use those to allow for several different types of messages to be passed to one instance of a handler. If there is only going to be one message that is passed back, then you don't have to worry about that.

    For example

    private static final int UPDATE_UI = 1;
    

    To send a message to the handler you call

    handler_.sendMessage(Message.obtain(handler_, UPDATE_UI, inputStreamInstance));
    

    From the handler:

    private Handler handler_ = new Handler(){
        @Override
    public void handleMessage(Message msg){
        switch(msg.what){
                case UPDATE_UI:
                InputStream is = (InputStream)msg.obj;
                //do what you need to with the InputStream
                break;
            }
        }
    
    };
    

    Alternatively, where the inputStreamInstance is added to the Message object, you can pass any object you like so if you wanted to parse it into some kind of container object or something like that, you could do that as well. Just cast it back to that object from within the handler.

    0 讨论(0)
  • 2020-12-02 17:56

    Try using AsyncTask. Goto this Link for more:

    private class SyncIncoData extends AsyncTask<String, String, String> {
        ProgressBar pb;
        ProgressBar pbar;
    
        @Override
        protected String doInBackground(String... urls) {
            for (int i = 0; i <= 100; i++) {
                publishProgress(i);
            }
            return null;
        }
    
        @Override
        protected void onPreExecute() {
    
            super.onPreExecute();
    
            pb = (ProgressBar) findViewById(R.id.progressBarsync4);
            pb.setVisibility(View.VISIBLE);
    
        }
    
        @Override
        protected void onPostExecute(String result) {
    
            pb = (ProgressBar) findViewById(R.id.progressBarsync4);
            pb.setVisibility(View.INVISIBLE);
    
        }
    
        @Override
        protected void onProgressUpdate(String... values) {
            pbar = (ProgressBar) findViewById(R.id.progressBarpbar);
            pbar.setProgress(Integer.parseInt(values[0]));
            super.onProgressUpdate(values);
        }
    }
    

    Write the program for http request inside doinbackgroud()

    0 讨论(0)
提交回复
热议问题