App crashes every time i make an http request

前端 未结 1 749
攒了一身酷
攒了一身酷 2021-01-24 18:17

This is my code every time i touch the imageview my app waits about 5 secs and then chrashes

I have the INTERNET permission

On the server side i have a php page

相关标签:
1条回答
  • 2021-01-24 18:46

    A logcat would be very helpful but its probably from doing network stuff on the UI. You should move all of your network code to a background Thread such as an AsyncTask. This will easily allow you to do the network stuff in the background then update the UI if needed in functions that run on the UI.

    AsyncTask Docs

    Here is an answer that shows the basic structure. Basically, you call the AsyncTask from the UI such as in your onClick() then you do network operations in doInBackground() which is called when the task first starts then you can update the UI in any of its other methods.

    Using the example I referenced, you would just put all of your network stuff, which looks like everything in your onClick(), inside the doInBackground() method of the example in the link. Then in your onClick() you would do something like

    lightbut.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
           TalkToServer task = new TalkToServer(); // could pass params to constructor here but might not be needed in this situation
           task.execute();  // could pass in params for `doInBackground()` such as url, etc... but again maybe not needed for this situation 
    }
    
    0 讨论(0)
提交回复
热议问题