Java Android: Calling a Method -


i'm building list view android application. want message returned when url clicked list in array telling user if site offline or online. doing quick ping returns boolean value this. java newbie , know way calling pinurl() method when remove said if statement onlistitemclick() app no crash.

here mainactivity reference:

package com.seven.webtools;  import java.io.ioexception; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url;  import android.annotation.suppresslint; import android.app.listactivity; import android.content.res.resources; import android.os.bundle; import android.view.menu; import android.view.view; import android.widget.arrayadapter; import android.widget.listview; import android.widget.toast;  @suppresslint("usevalueof") public class mainactivity extends listactivity {  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     resources res = getresources();     string[] sites = res.getstringarray(r.array.sites);     setlistadapter(new arrayadapter<string>(this,android.r.layout.simple_list_item_1,sites));  }  @override protected void onlistitemclick(listview l, view v, int position, long id) {     // selected url     super.onlistitemclick(l, v, position, id);     object o = this.getlistadapter().getitem(position);     string address = o.tostring();      //ping address     string status = "offline";     if (mainactivity.pingurl(address) == true){         status = "online";     }      //return result     toast.maketext(this, address + " " + status, toast.length_long).show(); }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.main, menu);     return true; }  public static boolean pingurl(final string address) {      try {       final url url = new url("http://" + address);       final httpurlconnection urlconn = (httpurlconnection) url.openconnection();       urlconn.setconnecttimeout(1000 * 10); // mtimeout in seconds       final long starttime = system.currenttimemillis();       urlconn.connect();       final long endtime = system.currenttimemillis();       if (urlconn.getresponsecode() == httpurlconnection.http_ok) {        system.out.println("time (ms) : " + (endtime - starttime));        system.out.println("ping "+address +" success");        return true;       }      } catch (final malformedurlexception e1) {       e1.printstacktrace();      } catch (final ioexception e) {       e.printstacktrace();      }      return false;     }  } 

thank if can me out :)

you should use either asynctask or thread , handler.

here's example of thread , handler:

public class mainactivity extends listactivity {     @override     protected void oncreate(bundle savedinstancestate)     {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         resources res = getresources();         string[] sites = res.getstringarray(r.array.sites);         setlistadapter(new arrayadapter<string>(this,android.r.layout.simple_list_item_1,sites));     }      // here create our handler receive message thread.     private handler handler = new handler()     {         @override         public void handlemessage(message msg)         {             stringbuilder str = new stringbuilder();             str.append((string) msg.obj).append("\r\n");             str.append("response:  " + msg.arg1).append("\r\n");             str.append("time: " + msg.arg2).append("\r\n");              system.out.println(str.tostring());         }     };      @override     protected void onlistitemclick(listview l, view v, int position, long id)     {         super.onlistitemclick(l, v, position, id);          final string address = l.getitematposition(position).tostring();          // create new thread , implements runnable interface         new thread(new runnable()         {             public void run()             {                 url url;                 httpurlconnection connection = null;                  try                 {                     url = new url(address);                      connection = (httpurlconnection) url.openconnection();                     connection.setconnecttimeout(10 * 1000);                     connection.setreadtimeout(10 * 1000);                      long start = system.currenttimemillis();                      connection.connect();                      int response = connection.getresponsecode();                      long end = system.currenttimemillis();                      // here create new message send handler object on activity                     //  send response code, ping time, , address                     message msg = message.obtain(handler);                     msg.arg1 = response;                     msg.arg2 = (int) (end - start);                     msg.obj = address;                     msg.sendtotarget();                 }                 catch(malformedurlexception e)                 {                     e.printstacktrace();                 }                 catch(ioexception e)                 {                     e.printstacktrace();                 }                                 {                     connection.disconnect();                 }             }         }).start();     } } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -