Hit API Through Volley
MainActivity.java -
package com.example.krishandev.loginapp; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; 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.StringRequest; import com.android.volley.toolbox.Volley; import org.json.JSONObject; import java.lang.reflect.Method; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.Map; import java.util.UUID; public class MainActivity extends AppCompatActivity { private RequestQueue requestQueue; private static final String JSON_URL ="http://mudraplan.arvaansolutions.com/api/checkNumberAlreadyExist"; EditText edit; Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edit=(EditText) findViewById(R.id.editText1); button =(Button)findViewById(R.id.button1); /* final Model model =new Model(); model.setMobileNumber(edit.getText().toString()); model.setDeviceUniqueId(UUID.randomUUID().toString()); */ button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(TextUtils.isEmpty(edit.getText()) || edit.length() != 10) Toast.makeText(getApplicationContext(),"please enter vali numbre",Toast.LENGTH_LONG).show(); else hitApi(); } }); } private void hitApi() { requestQueue= Volley.newRequestQueue(this); StringRequest stringRequest =new StringRequest(Request.Method.POST, JSON_URL, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.d("mritunjay",response); try{ JSONObject jsonObject= new JSONObject(response); if(jsonObject.getString("status").equals("success")){ Intent intentnt= new Intent(getApplicationContext(), LoginSuccessActivity.class); startActivity(intentnt); } }catch (Exception e){} } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }){ @Override protected Map<String,String> getParams() { Map<String, String> params = new HashMap<String, String>(); params.put("mobile_number",edit.getText().toString()); params.put("device_unique_id",UUID.randomUUID().toString()); return params; } }; requestQueue.add(stringRequest); } } -------------------------------------------------------------------------------------
activity_main.xml -
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/loginText1" android:text="Login Form" android:padding="10dp" android:textSize="20sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Phone Number" android:layout_below="@id/loginText1" android:id="@+id/textPhone" /> <EditText android:layout_width="250dp" android:layout_height="wrap_content" android:inputType="phone" android:layout_below="@id/textPhone" android:maxLength="12" android:id="@+id/editText1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Submit" android:layout_below="@id/editText1" android:id="@+id/button1" /> </RelativeLayout>----------------------------------------------------------------------------------Model.javapackage com.example.krishandev.loginapp; public class Model { String mobileNumber; int deviceUniqueId; public Model(String mobileNumber, int deviceUniqueId) { this.mobileNumber = mobileNumber; this.deviceUniqueId = deviceUniqueId; } public String getMobileNumber() { return mobileNumber; } public int getDeviceUniqueId() { return deviceUniqueId; } }-----------------------------------------------------------------------------LoginSuccessActivity.java-package com.example.krishandev.loginapp; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class LoginSuccessActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login_success); } }--------------------------------------------------------------------------activity_login_success.xml-<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".LoginSuccessActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Login Success" android:textSize="50sp" /> </android.support.constraint.ConstraintLayout>----------------------------------------------------Dependency-compile 'com.android.volley:volley:1.1.1'
Comments
Post a Comment