java - Callback in android(Network call in separate class) -
i have layout xml name main.xml , have main.java class in class send network call , data rest service.but want separate network call in anohther class.how can that.
main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".mainactivity" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <button android:id="@+id/callsubclass" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="call sub-class" /> <textview android:id="@+id/result" android:layout_width="match_parent" android:layout_height="wrap_content" /> </linearlayout>
main.java
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); buttoncallsubclass = (button)findviewbyid(r.id.callsubclass); textresult = (textview)findviewbyid(r.id.result); // can set network call here , update textview textresult .settext(result); }
rest service response ={"id":1958,"content":"hello, world!"}
my separate nework.java class
string url1 = string.format("http://rest-service.guides.spring.io/greeting"); jsonobjectrequest jsonobjreq = new jsonobjectrequest(request.method.get, url1, null, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { log.d("test", response.tostring()); string token="a"; string id=""; string result=""; try { id= response.get("id").tostring(); result= response.get("content").tostring(); }catch (jsonexception e){ e.printstacktrace(); toast.maketext(getapplicationcontext(), "error: " + e.getmessage(), toast.length_long).show(); } }
now want network call in separate class give solution.
Comments
Post a Comment