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
This is method we will be using to add parameters which we want to send to server.
@Override
protected Map
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
<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>
This comment has been removed by the author.
ReplyDeleteI 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