Showing posts with label Volley. Show all posts
Showing posts with label Volley. Show all posts

Monday, 11 May 2015

Posting Data on server Using Volley Android

This Tutorial let us know how to post data using volley. Here i will be calling a POST service which will accept parameters and returns a json result .

This is method we will be using to add parameters which we want to send to server.

 
@Override
protected Map getParams() throws AuthFailureError 
{
 // TODO Auto-generated method stub 
return super.getParams(); 



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.POST, url, null,  new Response.Listener<JSONObject>()
{                  
@Override                  
public void onResponse(JSONObject response)
{                
  Log.e("Response => ",response.toString());
 }              
}, new Response.ErrorListener()
{                  
@Override                  
public void onErrorResponse(VolleyError error)
{
                  
}              
})
  { //Code to send parameters to server
 @Override
 protected Map getParams() throws AuthFailureError 

  Map<String,String> params = new HashMap<String, String>();
 params.put("name","YOUR NAME VALUE"); 
 params.put("description", "YOUR description");
 params.put("price", "YOUR price"); 
 return params;
 } 
};        
queue.add(jsonObjReq);
 break; 
default: break; 
}
}



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>

Saturday, 9 May 2015

Using Volley to make web/Network Calls in Android

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

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:

What is Volley In Android ?

What is Volley ?

Volley is a library that help us making network calls in a easier and faster way in android.

Processing and Caching of network requests is managed by volley in a systematic way.

Volley Library network calls works asynchronously means no need to write Async Task.

Using Volley we can :

1. Request network calls.Response will be fetched by volley from web.

2. It provides memory caching.

3. Cancel Network request as well.

4. It provides Debugging tools as well.

5. Add Request to queue and prioritize.

6. Volley provides Memory Management as well.


So Volley is a networking library to make networking calls much easier, faster by adding requests.


We use Two classes of Volley i.e

1. Request Queue

2. Request


You can find how to use volley to make network calls in my next tutorial.

Cheers!!!!