android - okHttp :why my application is crashing didnt get response -


main activity

public class mainactivity extends appcompatactivity { string response ;

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     } 

public void click(view v) { getexample example = new getexample();

  try {       response = example.run();       toast.maketext(mainactivity.this, "response :" + response, toast.length_long).show();    } catch (ioexception e) {       toast.maketext(mainactivity.this, "error" + e, toast.length_long).show();     }   }  } 

getexample class

public class getexample { okhttpclient client;

public string run() throws ioexception {      client = new okhttpclient();     request request = new request.builder().url("http://grocit.pe.hu/getcategories.php").build();     response response = client.newcall(request).execute();     return response.body().string(); } } 

xml file

<relativelayout   android:layout_height="match_parent"    android:layout_width="match_parent"     xmlns:android="http://schemas.android.com/apk/res/android"> <button     android:layout_width="300dp"     android:layout_height="300dp"     android:onclick="click"     android:text="click me!"     /> </relativelayout> 

for such crashing questions can add crash logs. think crash occurs networkonmainthreadexception.this exception thrown when application attempts perform networking operation on main thread. networking operations must perform on thread :

    thread thread = new thread() {             @override             public void run() {                 // run method             }         };     thread.start(); 

Comments