This Tutorial let us know how to use volley. Here i will be calling a Get service which will accept no parameters and returns a json result that includes student info like name,email etc.
I will be using a model to store student data which is fetched from server.
Include Volley library in libs folder of your project,you can download the volley library online.
Project Structure goes like this
1. MainActivity.java
1. activity_main.xml
I will be using a model to store student data which is fetched from server.
Include Volley library in libs folder of your project,you can download the volley library online.
Project Structure goes like this
1. MainActivity.java
public class MainActivity extends Activity implements OnClickListener{ private String url = "Your URL"; private Button hitService; private ProgressDialog mDialog; private TextView mName,mEmail; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intiViews(); } private void intiViews() { hitService=(Button)findViewById(R.id.hitservice); hitService.setOnClickListener(this); mName=(TextView)findViewById(R.id.name); mEmail=(TextView)findViewById(R.id.email); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.hitservice: mDialog=new ProgressDialog(this); mDialog.setMessage("Loading"); mDialog.show(); RequestQueue queue = Volley.newRequestQueue(this); JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.e("Response => ",response.toString()); parseData(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); queue.add(jsonObjReq); break; default: break; } } private void parseData(JSONObject mResponse){ UserInfo mUserModel=new UserInfo(); try { mUserModel.setName(mResponse.getString("name")); mUserModel.setEmail(mResponse.getString("email")); setViewvalues(mUserModel); } catch (Exception e) { }finally{ mDialog.dismiss(); } } private void setViewvalues(UserInfo mUserModel) { mName.setText(mUserModel.getName()); mEmail.setText(mUserModel.getEmail()); } }2. UserInfo.java
public class UserInfo { String name="",email="",phone="",home="",mobile=""; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getHome() { return home; } public void setHome(String home) { this.home = home; } public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } }Define Internet Permission in manifest file
<uses-permission android:name="android.permission.INTERNET" />XML Layouts :
1. activity_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" > <Button android:id="@+id/hitservice" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hit Web Service" /> <TextView android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/email" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>Output:
No comments:
Post a Comment