诸如电子商务应用程序之类的许多应用程序都需要接受其用户的付款,以提供不同的产品或服务或为其用户提供服务。因此,此应用要求用户输入银行详细信息进行付款。在此支付网关中,要求用户添加其银行IFSC代码以获取其银行详细信息。如此众多的应用程序具有其应用程序内部的功能,以至于在输入银行IFSC代码时,会从该IFSC代码中获取用户的银行详细信息,例如银行地址,银行城市和其他常见详细信息。因此,在本文中,我们将研究如何从Android的IFSC代码中获取通用银行详细信息。
我们将在本文中构建什么?
我们将构建一个简单的应用程序,在该应用程序中,我们将通过EditText从用户那里获取IFSC代码,然后,用户必须单击一个简单的按钮以从该IFSC代码中获取数据,例如银行地址,银行MICR代码,联系电话和其他详细信息。为了执行此任务,我们将使用一个简单的API,并将其添加到我们的应用程序中。此应用程序将向我们提供来自与银行有关的API的基本数据。下面是GIF图像,在该图像中,我们将看到本文将要构建的内容。注意,我们将使用Java语言实现该项目。
分步实施
步骤1:在Android Studio中创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。
步骤2:在您的build.gradle文件中添加以下依赖项
以下是Volley的依赖关系,我们将使用它们来从API获取数据。要添加此依赖关系,请导航至应用程序> Gradle脚本> build.gradle(app),然后在“依赖关系”部分添加以下依赖关系。
implementation ‘com.android.volley:volley:1.1.1’
添加此依赖项后,同步您的项目,现在移至XML部分。
步骤3:使用activity_main.xml文件
转到activity_main.xml文件,并参考以下代码。以下是activity_main.xml文件的代码。
XML
Java
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
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 org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
// creating variables for edit text
// and our text views.
private EditText ifscCodeEdt;
private TextView bankDetailsTV;
// creating a variable for
// our ifsc code string.
String ifscCode;
// creating a variable for request queue.
private RequestQueue mRequestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initializing our variables.
ifscCodeEdt = findViewById(R.id.idedtIfscCode);
Button getBankDetailsBtn = findViewById(R.id.idBtnGetBankDetails);
bankDetailsTV = findViewById(R.id.idTVBankDetails);
// initializing our request que variable with request queue
// and passing our context to it.
mRequestQueue = Volley.newRequestQueue(MainActivity.this);
// initialing on click listener for our button.
getBankDetailsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// getting string from edittext.
ifscCode = ifscCodeEdt.getText().toString();
// validating if the edit text
// is empty or not.
if (TextUtils.isEmpty(ifscCode)) {
// displaying a toast message if the text field is empty
Toast.makeText(MainActivity.this, "Please enter valid IFSC code", Toast.LENGTH_SHORT).show();
} else {
// calling a method to display
// our ifsc code details.
getDataFromIFSCCode(ifscCode);
}
}
});
}
private void getDataFromIFSCCode(String ifscCode) {
// clearing our cache of request queue.
mRequestQueue.getCache().clear();
// below is the url from where we will be getting
// our response in the json format.
String url = "http://api.techm.co.in/api/v1/ifsc/" + ifscCode;
// below line is use to initialize our request queue.
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
// creating a json object request for our API.
JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
// this method is used to get
// the response from the API.
try {
if (response.getString("status").equals("failed")) {
// checking if the response is not loaded and
// status for the repose is fail.
// if response status is failure we are displaying
// an invalid IFSC code in our text view.
bankDetailsTV.setText("Invalid IFSC Code");
} else {
// if the status is successful we are
// extracting data from JSON file
JSONObject dataObj = response.getJSONObject("data");
String state = dataObj.optString("STATE");
String bankName = dataObj.optString("BANK");
String branch = dataObj.optString("BRANCH");
String address = dataObj.optString("ADDRESS");
String contact = dataObj.optString("CONTACT");
String micrcode = dataObj.optString("MICRCODE");
String city = dataObj.optString("CITY");
// after extracting this data we are displaying
// that data in our text view.
bankDetailsTV.setText("Bank Name : " + bankName + "\nBranch : " + branch + "\nAddress : " + address + "\nMICR Code : " + micrcode + "\nCity : " + city + "\nState : " + state + "\nContact : " + contact);
}
} catch (JSONException e) {
// if we get any error while loading data
// we are setting our text as invalid IFSC code.
e.printStackTrace();
bankDetailsTV.setText("Invalid IFSC Code");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// if we get any error while loading json
// data we are setting our text to invalid IFSC code.
bankDetailsTV.setText("Invalid IFSC Code");
}
});
// below line is use for adding object
// request to our request queue.
queue.add(objectRequest);
}
}
XML
步骤4:使用MainActivity。 Java文件
转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。
Java
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
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 org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
// creating variables for edit text
// and our text views.
private EditText ifscCodeEdt;
private TextView bankDetailsTV;
// creating a variable for
// our ifsc code string.
String ifscCode;
// creating a variable for request queue.
private RequestQueue mRequestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initializing our variables.
ifscCodeEdt = findViewById(R.id.idedtIfscCode);
Button getBankDetailsBtn = findViewById(R.id.idBtnGetBankDetails);
bankDetailsTV = findViewById(R.id.idTVBankDetails);
// initializing our request que variable with request queue
// and passing our context to it.
mRequestQueue = Volley.newRequestQueue(MainActivity.this);
// initialing on click listener for our button.
getBankDetailsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// getting string from edittext.
ifscCode = ifscCodeEdt.getText().toString();
// validating if the edit text
// is empty or not.
if (TextUtils.isEmpty(ifscCode)) {
// displaying a toast message if the text field is empty
Toast.makeText(MainActivity.this, "Please enter valid IFSC code", Toast.LENGTH_SHORT).show();
} else {
// calling a method to display
// our ifsc code details.
getDataFromIFSCCode(ifscCode);
}
}
});
}
private void getDataFromIFSCCode(String ifscCode) {
// clearing our cache of request queue.
mRequestQueue.getCache().clear();
// below is the url from where we will be getting
// our response in the json format.
String url = "http://api.techm.co.in/api/v1/ifsc/" + ifscCode;
// below line is use to initialize our request queue.
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
// creating a json object request for our API.
JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
// this method is used to get
// the response from the API.
try {
if (response.getString("status").equals("failed")) {
// checking if the response is not loaded and
// status for the repose is fail.
// if response status is failure we are displaying
// an invalid IFSC code in our text view.
bankDetailsTV.setText("Invalid IFSC Code");
} else {
// if the status is successful we are
// extracting data from JSON file
JSONObject dataObj = response.getJSONObject("data");
String state = dataObj.optString("STATE");
String bankName = dataObj.optString("BANK");
String branch = dataObj.optString("BRANCH");
String address = dataObj.optString("ADDRESS");
String contact = dataObj.optString("CONTACT");
String micrcode = dataObj.optString("MICRCODE");
String city = dataObj.optString("CITY");
// after extracting this data we are displaying
// that data in our text view.
bankDetailsTV.setText("Bank Name : " + bankName + "\nBranch : " + branch + "\nAddress : " + address + "\nMICR Code : " + micrcode + "\nCity : " + city + "\nState : " + state + "\nContact : " + contact);
}
} catch (JSONException e) {
// if we get any error while loading data
// we are setting our text as invalid IFSC code.
e.printStackTrace();
bankDetailsTV.setText("Invalid IFSC Code");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// if we get any error while loading json
// data we are setting our text to invalid IFSC code.
bankDetailsTV.setText("Invalid IFSC Code");
}
});
// below line is use for adding object
// request to our request queue.
queue.add(objectRequest);
}
}
步骤5:在清单文件中添加Internet权限
导航至应用程序> AndroidManifest.xml文件,然后向其添加以下权限。
XML格式
添加此权限之后。运行您的项目,然后在下面的屏幕上查看输出。