PAM Lab 3 Rotaru Dan
PAM Lab 3 Rotaru Dan
RAPORT
Lucrare de laborator Nr.3
Disciplina: Programarea
aplicațiilor mobile
Tema: Simple HTTP Client (Web Service Emulation)
Chișinău 2021
Scopul lucrării de laborator
package com.danrotaru.pamlab3;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Locale;
// API: https://github.com/fawazahmed0/currency-api#readme
// Response example: https://cdn.jsdelivr.net/gh/fawazahmed0/currency-
api@1/latest/currencies/eur/mdl.json
final String API = "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-
api@1/latest/currencies/";
public int c1 = 0;
public int c2 = 2;
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
reorder.setOnClickListener(v -> {
int temp;
temp = c1;
c1 = c2;
currency1.setText(currencyList[c2]);
c2 = temp;
currency2.setText(currencyList[temp]);
Convert(true);
});
@SuppressLint("SetTextI18n")
public void Convert(boolean type) {
String text;
if(!isNetworkAvailable()){
Toast.makeText(MainActivity.this, getString(R.string.CheckInternet),
Toast.LENGTH_LONG).show();
}
else{
try {
text =
getRequest(API+currencyList[c1].toLowerCase(Locale.ROOT)+"/"+currencyList[c2].toLowerCase(
Locale.ROOT)+".json");
JSONObject obj;
try {
obj = new JSONObject(text);
String val = obj.getString(currencyList[c2].toLowerCase(Locale.ROOT));
EditText am = findViewById(R.id.amount);
String Amount = am.getText().toString();
float res = 0;
if(!Amount.isEmpty()){
res = Float.parseFloat(Amount) * Float.parseFloat(val);
res = (float) (Math.round(res*100.0)/100.0);
}
if(type){
if(!Amount.isEmpty() && !currencyList[c1].equals(currencyList[c2])){
TextView resultText = findViewById(R.id.result);
resultText.setVisibility(View.VISIBLE);
resultText.setText(Amount + " " + currencyList[c1] + " = " + res + " " +
currencyList[c2]);
}
}
else if(Amount.isEmpty()){
Toast.makeText(MainActivity.this, getString(R.string.ValidSum),
Toast.LENGTH_LONG).show();
}
else if(currencyList[c1].equals(currencyList[c2])){
Toast.makeText(MainActivity.this, getString(R.string.SameCurrency),
Toast.LENGTH_LONG).show();
}
else{
TextView resultText = findViewById(R.id.result);
resultText.setVisibility(View.VISIBLE);
resultText.setText(Amount + " " + currencyList[c1] + " = " + res + " " +
currencyList[c2]);
}
} catch (JSONException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}