1

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 that reads the GET and insert it in a database

public class Home extends Activity {
ImageView lightbut;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    ImageView lightbut = (ImageView) findViewById(R.id.light);
    lightbut.setClickable(true); 
    lightbut.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("== My activity ===","OnClick is called");
         // Creating HTTP client
            HttpClient httpClient = new DefaultHttpClient();

            // Creating HTTP Post
            HttpGet httpPost = new HttpGet("http://192.168.0.102/HR/index.php?command=ligthsoff");
            try {
                HttpResponse response = httpClient.execute(httpPost);


            } catch (ClientProtocolException e) {
                // writing exception to log
                e.printStackTrace();

            } catch (IOException e) {
                // writing exception to log
                e.printStackTrace();
            }
        }
    });
}
Christos Mitsis
  • 167
  • 1
  • 3
  • 17
  • For ease of use, check out [droidQuery](http://bit.ly/droidquery). Getting `XML` or `JSON` is as simple as `$.getJSON(url)` or `$.getXML(url)`. – Phil Aug 22 '13 at 19:41
  • These days you should use Android's Volley library https://developer.android.com/training/volley/index.html – Burrito Aug 12 '17 at 20:50

1 Answers1

3

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 
}
Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • How can i do that? If you gave me a code i would be very thankful! – Christos Mitsis Aug 22 '13 at 18:46
  • I have edited with a link to an example of getting started with it. You also should read the docs because there is important information you need to understand. – codeMagic Aug 22 '13 at 18:48
  • I have edited again with a little more of a description. That should get you started. You can simply make the `AsyncTask` an inner class of your `Activity` if it won't be needed anywhere else. Then you can have access to member variables of the `Activity` as well as access to its functions – codeMagic Aug 22 '13 at 18:54