
This Tutorial will example program for Volley Library. Here am using “POST” method for this example. For Json just click here
Video
for Volley library we need one supported gradle that we need to add in gradle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
apply plugin: 'com.android.application' android { compileSdkVersion 29 buildToolsVersion "29.0.3" defaultConfig { applicationId "com.androindian.volleyreg" minSdkVersion 16 targetSdkVersion 29 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { dataBinding.enabled=true minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' implementation 'com.android.volley:volley:1.1.1' } |
First we need to Design the screens for Registration and Login
Here i did not design user interface directly i sent manual values so you can design your screens and instead of manual you can send those values.
Activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<?xml version="1.0" encoding="utf-8"?> <layout> <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" tools:context=".MainActivity" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Welcome" android:gravity="center"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Name" android:id="@+id/name"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Email" android:id="@+id/email"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Mobile Number" android:inputType="number" android:id="@+id/mobile"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:inputType="textPassword" android:id="@+id/pass"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Register" android:id="@+id/re"/> </LinearLayout> </layout> |
mainactivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
package com.androindian.volleyreg; import androidx.appcompat.app.AppCompatActivity; import androidx.databinding.DataBindingUtil; import android.os.Bundle; import android.view.View; import android.widget.Toast; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.Volley; import com.androindian.volleyreg.databinding.ActivityMainBinding; import org.json.JSONException; import org.json.JSONObject; public class MainActivity extends AppCompatActivity { ActivityMainBinding binding; //1 RequestQueue requestQueue; //7 String url="http://androindian.com/apps/example_app/api.php"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = DataBindingUtil.setContentView(MainActivity.this, R.layout.activity_main); //2 requestQueue = Volley.newRequestQueue(MainActivity.this); binding.re.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //3 JSONObject jsonObject=new JSONObject(); try { jsonObject.put("name",binding.name.getText().toString().trim()); jsonObject.put("mobile",binding.mobile.getText().toString().trim()); jsonObject.put("email",binding.email.getText().toString().trim()); jsonObject.put("pswrd",binding.pass.getText().toString().trim()); jsonObject.put("baction","register_user"); } catch (JSONException e) { e.printStackTrace(); } //4 JsonObjectRequest objectRequest=new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { //8 try { String res=response.getString("response"); if(res.equalsIgnoreCase("failed")){ String res1=response.getString("user"); Toast.makeText(MainActivity.this, res1, Toast.LENGTH_SHORT).show(); }else if(res.equalsIgnoreCase("success")){ String res1=response.getString("user"); Toast.makeText(MainActivity.this, res1, Toast.LENGTH_SHORT).show(); }else { String res1=response.getString("user"); Toast.makeText(MainActivity.this, res1, Toast.LENGTH_SHORT).show(); } } catch (JSONException e) { e.printStackTrace(); } } //5 }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(MainActivity.this, ""+error.toString(), Toast.LENGTH_SHORT).show(); } }); //6 requestQueue.add(objectRequest); } }); } } |
Android manifest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.androindian.volleyreg"> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <application android:allowBackup="true" android:usesCleartextTraffic="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |