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>

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work. job posting site

    ReplyDelete