How to USE web service to build Currency converter app-android
1.Add OKHTTP dependency to your build.gradle and sync project.
implementation 'com.squareup.okhttp:okhttp:2.5.0'
2.Design your Currency app view
XML file is mentioned below and copy it to activity.main.xml file.
<?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"><RelativeLayoutandroid:id="@+id/relativeLayout"android:layout_width="match_parent"android:layout_height="485dp"android:layout_alignParentEnd="true"android:layout_alignParentBottom="true"android:layout_marginEnd="0dp"android:layout_marginBottom="-53dp"android:background="@android:color/holo_blue_dark"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.0"app:layout_constraintStart_toStartOf="parent"tools:layout_editor_absoluteY="197dp"><LinearLayoutandroid:layout_width="183dp"android:layout_height="wrap_content"android:layout_alignParentStart="true"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:layout_marginStart="0dp"android:layout_marginLeft="0dp"android:layout_marginTop="58dp"android:orientation="vertical"><TextViewandroid:id="@+id/textView"android:layout_width="157dp"android:layout_height="wrap_content"android:text="From Currency"android:textColor="@android:color/black"android:textSize="18sp"android:textStyle="bold"tools:layout_marginLeft="5dp" /></LinearLayout><LinearLayoutandroid:layout_width="183dp"android:layout_height="wrap_content"android:layout_alignParentStart="true"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:layout_marginStart="0dp"android:layout_marginLeft="0dp"android:layout_marginTop="87dp"android:orientation="vertical"><TextViewandroid:id="@+id/textView5"android:layout_width="183dp"android:layout_height="match_parent"android:text="To Currency"android:textColor="@android:color/black"android:textSize="18sp"android:textStyle="bold"tools:layout_marginLeft="5dp" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="132dp"android:layout_alignParentStart="true"android:layout_alignParentLeft="true"android:layout_alignParentBottom="true"android:layout_marginStart="0dp"android:layout_marginLeft="0dp"android:layout_marginBottom="225dp"android:orientation="vertical"><TextViewandroid:id="@+id/textView6"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Enter Exchange amout"android:textColor="@android:color/black"android:textSize="14sp"android:textStyle="bold" /><EditTextandroid:id="@+id/editText"android:layout_width="match_parent"android:layout_height="wrap_content"android:ems="10"android:inputType="numberDecimal" /><TextViewandroid:id="@+id/textView8"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Converted Amount"android:textColor="@android:color/black"android:textSize="14sp"android:textStyle="bold" /><EditTextandroid:id="@+id/editText4"android:layout_width="match_parent"android:layout_height="wrap_content"android:ems="10"android:inputType="numberDecimal" /></LinearLayout><LinearLayoutandroid:layout_width="171dp"android:layout_height="wrap_content"android:layout_alignParentStart="true"android:layout_alignParentLeft="true"android:layout_alignParentBottom="true"android:layout_marginStart="186dp"android:layout_marginLeft="186dp"android:layout_marginBottom="147dp"android:orientation="vertical"><Buttonandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Calculate"android:textColor="@android:color/white"android:background="@color/black" /></LinearLayout><EditTextandroid:id="@+id/editText5"android:layout_width="400dp"android:layout_height="wrap_content"android:layout_alignParentStart="true"android:layout_alignParentLeft="true"android:layout_marginStart="37dp"android:layout_marginLeft="37dp"android:layout_marginBottom="-47dp"android:ems="10"android:inputType="textPersonName"android:text="Welcome to Currency Convert"android:textColor="@android:color/white"android:textSize="24sp"android:textStyle="bold" /><Spinnerandroid:id="@+id/planets_spinner"android:layout_width="105dp"android:layout_height="wrap_content"android:layout_alignBottom="@+id/constraintID1"android:layout_alignParentEnd="true"android:layout_alignParentRight="true"android:layout_marginEnd="60dp"android:layout_marginRight="60dp"android:layout_marginBottom="-2dp" /><android.support.constraint.ConstraintLayoutandroid:id="@+id/constraintID1"android:layout_width="129dp"android:layout_height="24dp"android:layout_alignParentTop="true"android:layout_alignParentEnd="true"android:layout_alignParentRight="true"android:layout_marginTop="89dp"android:layout_marginEnd="34dp"android:layout_marginRight="34dp" /><android.support.constraint.ConstraintLayoutandroid:layout_width="127dp"android:layout_height="27dp"android:layout_alignParentTop="true"android:layout_alignParentEnd="true"android:layout_alignParentRight="true"android:layout_marginTop="56dp"android:layout_marginEnd="47dp"android:layout_marginRight="47dp"><TextViewandroid:id="@+id/textView14"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="16dp"android:layout_marginLeft="16dp"android:text="Euro"android:textColor="@android:color/background_dark"android:textStyle="bold"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /></android.support.constraint.ConstraintLayout></RelativeLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="251dp"android:orientation="vertical"tools:layout_editor_absoluteX="8dp"tools:layout_editor_absoluteY="8dp"><ImageViewandroid:layout_width="match_parent"android:layout_height="233dp"android:src="@drawable/abcd"/></LinearLayout></RelativeLayout>
3.Add below code to your MainActivity.java file
Here we have called the API using web service and get the data as JSON and save it to JSON array and then we are linking the data with the spinner drop down and as the user selected the relevant calculations were done.
we are using web service of
https://api.exchangeratesapi.io/latest
package com.example.cssuser.currencyconvert;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.Spinner;import android.widget.TextView;import android.widget.Toast;import com.squareup.okhttp.Call;import com.squareup.okhttp.Callback;import com.squareup.okhttp.OkHttpClient;import com.squareup.okhttp.Request;import com.squareup.okhttp.Response;import org.json.JSONException;import org.json.JSONObject;import java.io.IOException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class MainActivity extends AppCompatActivity {List<String> keysList;Spinner toCurrency;EditText txt;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);toCurrency = (Spinner)findViewById(R.id.planets_spinner);final EditText euroVal = (EditText)findViewById(R.id.editText);final Button converter= (Button)findViewById(R.id.button);txt = (EditText)findViewById(R.id.editText4);try {loadConvTypes();} catch (IOException e) {e.printStackTrace();}converter.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if(!euroVal.getText().toString().isEmpty()){String toCurr = toCurrency.getSelectedItem().toString();double euroVlaue = Double.valueOf(euroVal.getText().toString());try {convertCurrency(toCurr, euroVlaue);} catch (IOException e) {e.printStackTrace();}}else{Toast.makeText(MainActivity.this, "Please Enter a Value",Toast.LENGTH_SHORT).show();}}});}public void loadConvTypes() throws IOException {String url = "https://api.exchangeratesapi.io/latest";OkHttpClient client = new OkHttpClient();Request request = new Request.Builder().url(url).header("Content-Type", "application/json").build();client.newCall(request).enqueue(new Callback() {@Overridepublic void onFailure(Request request, IOException e) {String mMessage = e.getMessage().toString();Log.w("failure Response", mMessage);Toast.makeText(MainActivity.this, mMessage, Toast.LENGTH_SHORT).show();}@Overridepublic void onResponse(Response response) throws IOException {final String mMessage = response.body().string();MainActivity.this.runOnUiThread(new Runnable() {@Overridepublic void run() {//Toast.makeText(MainActivity.this, mMessage, Toast.LENGTH_SHORT).show();try {JSONObject obj = new JSONObject(mMessage);JSONObject b = obj.getJSONObject("rates");Iterator keysToCopyIterator = b.keys();keysList = new ArrayList<String>();while(keysToCopyIterator.hasNext()) {String key = (String) keysToCopyIterator.next();keysList.add(key);}ArrayAdapter<String> spinnerArrayAdapter = newArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, keysList );toCurrency.setAdapter(spinnerArrayAdapter);} catch (JSONException e) {e.printStackTrace();}}});}});}public void convertCurrency(final String toCurr, final double euroVlaue) throwsIOException {String url = "https://api.exchangeratesapi.io/latest";OkHttpClient client = new OkHttpClient();Request request = new Request.Builder().url(url).header("Content-Type", "application/json").build();client.newCall(request).enqueue(new Callback() {@Overridepublic void onFailure(Request request, IOException e) {String mMessage = e.getMessage().toString();Log.w("failure Response", mMessage);Toast.makeText(MainActivity.this, mMessage, Toast.LENGTH_SHORT).show();}@Overridepublic void onResponse(Response response) throws IOException {final String mMessage = response.body().string();MainActivity.this.runOnUiThread(new Runnable() {@Overridepublic void run() {try {JSONObject obj = new JSONObject(mMessage);JSONObject b = obj.getJSONObject("rates");String val = b.getString(toCurr);double output = euroVlaue*Double.valueOf(val);txt.setText(String.valueOf(output));} catch (JSONException e) {e.printStackTrace();}}});}});}}
Thank you !!!!
feedbacks are appriciated.
