Add files via upload

This commit is contained in:
2017-10-05 15:23:51 +01:00
committed by GitHub
parent f478affe76
commit 62b6fb2ed5

View File

@@ -1,312 +1,323 @@
package com.example.haimalik.myapplication; package com.example.haimalik.myapplication;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.text.Editable; import android.text.Editable;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Spinner; import android.widget.ProgressBar;
import android.widget.AdapterView.OnItemSelectedListener; import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException; import java.io.BufferedReader;
import java.io.InputStream; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStream;
import java.net.URL; import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.URL;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.nio.charset.Charset; import java.net.MalformedURLException;
import java.text.DecimalFormat; import java.nio.charset.Charset;
import java.text.DecimalFormat;
public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity {
EditText currencyOneEditText;
EditText currencyTwoEditText; EditText currencyOneEditText;
Spinner spinnerTop; EditText currencyTwoEditText;
Spinner spinnerBottom; Spinner spinnerTop;
double conversionRateOne; Spinner spinnerBottom;
double conversionRateOne;
private String URL = "https://free.currencyconverterapi.com/api/v3/convert?"; ProgressBar spinner;
private static final String LOG_TAG = MainActivity.class.getSimpleName(); private String URL = "https://free.currencyconverterapi.com/api/v3/convert?";
private static final String LOG_TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); @Override
setContentView(R.layout.activity_main); protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
currencyOneEditText = (EditText) findViewById(R.id.editText); setContentView(R.layout.activity_main);
currencyTwoEditText = (EditText) findViewById(R.id.editText2);
currencyOneEditText = (EditText) findViewById(R.id.editText);
currencyOneEditText.addTextChangedListener(TextWatcherClass); currencyTwoEditText = (EditText) findViewById(R.id.editText2);
currencyTwoEditText.addTextChangedListener(TextWatcherClass2);
currencyOneEditText.addTextChangedListener(TextWatcherClass);
currencyTwoEditText.addTextChangedListener(TextWatcherClass2);
addListenerOnSpinnerItemSelection(); spinner = (ProgressBar) findViewById(R.id.progressBar);
spinner.setVisibility(View.GONE);
String stringURL = UriBuilder();
MyAsyncTask task = new MyAsyncTask(); addListenerOnSpinnerItemSelection();
task.execute(stringURL);
} String stringURL = UriBuilder();
MyAsyncTask task = new MyAsyncTask();
task.execute(stringURL);
private TextWatcher TextWatcherClass = new TextWatcher() { }
@Override
public void onTextChanged(CharSequence s, int start, int before, private TextWatcher TextWatcherClass = new TextWatcher() {
int count) {
currencyTwoEditText.removeTextChangedListener(TextWatcherClass2); @Override
if(currencyOneEditText.getText().toString().isEmpty()){ public void onTextChanged(CharSequence s, int start, int before,
currencyTwoEditText.setText(""); int count) {
return; currencyTwoEditText.removeTextChangedListener(TextWatcherClass2);
} if(currencyOneEditText.getText().toString().isEmpty()){
currencyTwoEditText.setText("");
} return;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, }
int after) {
} @Override
public void beforeTextChanged(CharSequence s, int start, int count,
@Override int after) {
public void afterTextChanged(Editable s) { }
try{ @Override
public void afterTextChanged(Editable s) {
Double topValue = Double.parseDouble(currencyOneEditText.getText().toString());
Double bottomValue = topValue * conversionRateOne; try{
DecimalFormat df = new DecimalFormat("#.##");
bottomValue = Double.valueOf(df.format(bottomValue)); Double topValue = Double.parseDouble(currencyOneEditText.getText().toString());
currencyTwoEditText.setText(bottomValue.toString()); Double bottomValue = topValue * conversionRateOne;
DecimalFormat df = new DecimalFormat("#.##");
bottomValue = Double.valueOf(df.format(bottomValue));
} currencyTwoEditText.setText(bottomValue.toString());
catch (NumberFormatException e){
Log.e(LOG_TAG, "no numbers inserted");
} }
currencyTwoEditText.addTextChangedListener(TextWatcherClass2); catch (NumberFormatException e){
} Log.e(LOG_TAG, "no numbers inserted");
}
}; currencyTwoEditText.addTextChangedListener(TextWatcherClass2);
}
private TextWatcher TextWatcherClass2 = new TextWatcher() {
};
@Override
public void onTextChanged(CharSequence s, int start, int before, private TextWatcher TextWatcherClass2 = new TextWatcher() {
int count) {
currencyOneEditText.removeTextChangedListener(TextWatcherClass); @Override
if(currencyTwoEditText.getText().toString().isEmpty()){ public void onTextChanged(CharSequence s, int start, int before,
currencyOneEditText.setText(""); int count) {
} currencyOneEditText.removeTextChangedListener(TextWatcherClass);
} if(currencyTwoEditText.getText().toString().isEmpty()){
currencyOneEditText.setText("");
@Override }
public void beforeTextChanged(CharSequence s, int start, int count, }
int after) {
} @Override
public void beforeTextChanged(CharSequence s, int start, int count,
@Override int after) {
public void afterTextChanged(Editable s) { }
try{ @Override
public void afterTextChanged(Editable s) {
Double bottomValue = Double.parseDouble(currencyTwoEditText.getText().toString());
Double topValue = bottomValue * (1 / conversionRateOne); try{
DecimalFormat df = new DecimalFormat("#.##");
topValue = Double.valueOf(df.format(topValue)); Double bottomValue = Double.parseDouble(currencyTwoEditText.getText().toString());
currencyOneEditText.setText(topValue.toString()); Double topValue = bottomValue * (1 / conversionRateOne);
DecimalFormat df = new DecimalFormat("#.##");
topValue = Double.valueOf(df.format(topValue));
} currencyOneEditText.setText(topValue.toString());
catch (NumberFormatException e){
Log.e(LOG_TAG, "no numbers inserted");
} }
currencyOneEditText.addTextChangedListener(TextWatcherClass); catch (NumberFormatException e){
} Log.e(LOG_TAG, "no numbers inserted");
}
}; currencyOneEditText.addTextChangedListener(TextWatcherClass);
}
public void addListenerOnSpinnerItemSelection() {
spinnerTop = (Spinner) findViewById(R.id.spinner1); };
spinnerTop.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override public void addListenerOnSpinnerItemSelection() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int pos, long id) { spinnerTop = (Spinner) findViewById(R.id.spinner1);
String currencyOne = parentView.getItemAtPosition(pos).toString(); spinnerTop.setOnItemSelectedListener(new OnItemSelectedListener() {
currencyTwoEditText.setText(""); @Override
currencyOneEditText.setText(""); public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int pos, long id) {
currencyOneEditText.setHint("insert " + currencyOne); String currencyOne = parentView.getItemAtPosition(pos).toString();
currencyTwoEditText.setText("");
String stringURL = UriBuilder(); currencyOneEditText.setText("");
MyAsyncTask task = new MyAsyncTask(); currencyOneEditText.setHint("insert " + currencyOne);
task.execute(stringURL);
String stringURL = UriBuilder();
} MyAsyncTask task = new MyAsyncTask();
task.execute(stringURL);
@Override
public void onNothingSelected(AdapterView<?> parentView) { }
// your code here
} @Override
public void onNothingSelected(AdapterView<?> parentView) {
}); // your code here
}
spinnerBottom = (Spinner) findViewById(R.id.spinner2);
spinnerBottom.setOnItemSelectedListener(new OnItemSelectedListener() { });
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int pos, long id) { spinnerBottom = (Spinner) findViewById(R.id.spinner2);
String currencyTwo = parentView.getItemAtPosition(pos).toString(); spinnerBottom.setOnItemSelectedListener(new OnItemSelectedListener() {
currencyOneEditText.setText(""); @Override
currencyTwoEditText.setText(""); public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int pos, long id) {
currencyTwoEditText.setHint("insert " + currencyTwo); String currencyTwo = parentView.getItemAtPosition(pos).toString();
currencyOneEditText.setText("");
String stringURL = UriBuilder(); currencyTwoEditText.setText("");
MyAsyncTask task = new MyAsyncTask(); currencyTwoEditText.setHint("insert " + currencyTwo);
task.execute(stringURL);
} String stringURL = UriBuilder();
MyAsyncTask task = new MyAsyncTask();
@Override task.execute(stringURL);
public void onNothingSelected(AdapterView<?> parentView) { }
// your code here
} @Override
public void onNothingSelected(AdapterView<?> parentView) {
}); // your code here
}
}
});
private String UriBuilder(){
String currencyOne = spinnerTop.getSelectedItem().toString(); }
String currencyTwo = spinnerBottom.getSelectedItem().toString();
private String UriBuilder(){
Uri baseUri = Uri.parse(URL); String currencyOne = spinnerTop.getSelectedItem().toString();
Uri.Builder builder = baseUri.buildUpon(); String currencyTwo = spinnerBottom.getSelectedItem().toString();
builder.appendQueryParameter("q", currencyOne + "_" + currencyTwo).appendQueryParameter("compact", "ultra");
Uri baseUri = Uri.parse(URL);
return builder.build().toString(); Uri.Builder builder = baseUri.buildUpon();
builder.appendQueryParameter("q", currencyOne + "_" + currencyTwo).appendQueryParameter("compact", "ultra");
}
return builder.build().toString();
private static URL createUrl(String stringUrl) {
URL url = null; }
try {
url = new URL(stringUrl); private static URL createUrl(String stringUrl) {
} catch (MalformedURLException e) { URL url = null;
Log.e(LOG_TAG, "Error with creating URL ", e); try {
} url = new URL(stringUrl);
return url; } catch (MalformedURLException e) {
} Log.e(LOG_TAG, "Error with creating URL ", e);
}
return url;
private static String makeHttpRequest(URL url) throws IOException { }
String jsonResponse = "";
if (url == null) { private static String makeHttpRequest(URL url) throws IOException {
return jsonResponse; String jsonResponse = "";
}
if (url == null) {
HttpURLConnection urlConnection = null; return jsonResponse;
InputStream inputStream = null; }
try {
urlConnection = (HttpURLConnection) url.openConnection(); HttpURLConnection urlConnection = null;
urlConnection.setReadTimeout(30000); InputStream inputStream = null;
urlConnection.setConnectTimeout(30000); try {
urlConnection.setRequestMethod("GET"); urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect(); urlConnection.setReadTimeout(30000);
urlConnection.setConnectTimeout(30000);
if (urlConnection.getResponseCode() == 200) { urlConnection.setRequestMethod("GET");
inputStream = urlConnection.getInputStream(); urlConnection.connect();
jsonResponse = readFromStream(inputStream);
} else { if (urlConnection.getResponseCode() == 200) {
Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode()); inputStream = urlConnection.getInputStream();
} jsonResponse = readFromStream(inputStream);
} catch (IOException e) { } else {
Log.e(LOG_TAG, "Problem retrieving the JSON results.", e); Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode());
} finally { }
if (urlConnection != null) { } catch (IOException e) {
urlConnection.disconnect(); Log.e(LOG_TAG, "Problem retrieving the JSON results.", e);
} } finally {
if (inputStream != null) { if (urlConnection != null) {
inputStream.close(); urlConnection.disconnect();
} }
} if (inputStream != null) {
return jsonResponse; inputStream.close();
} }
}
private static String readFromStream(InputStream inputStream) throws IOException { return jsonResponse;
StringBuilder output = new StringBuilder(); }
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8")); private static String readFromStream(InputStream inputStream) throws IOException {
BufferedReader reader = new BufferedReader(inputStreamReader); StringBuilder output = new StringBuilder();
String line = ""; if (inputStream != null) {
while (line != null) { InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
output.append(line); BufferedReader reader = new BufferedReader(inputStreamReader);
line = reader.readLine(); String line = "";
} while (line != null) {
} output.append(line);
Log.d(LOG_TAG, output.toString()); line = reader.readLine();
return output.toString(); }
}
} Log.d(LOG_TAG, output.toString());
return output.toString();
private double extractFeatureFromJson(String newsJSON) {
double conversionValue = 0.00; }
String currencyOne = spinnerTop.getSelectedItem().toString();
String currencyTwo = spinnerBottom.getSelectedItem().toString(); private double extractFeatureFromJson(String newsJSON) {
double conversionValue = 0.00;
if (TextUtils.isEmpty(newsJSON)) { String currencyOne = spinnerTop.getSelectedItem().toString();
return 0.00; String currencyTwo = spinnerBottom.getSelectedItem().toString();
}
if (TextUtils.isEmpty(newsJSON)) {
try { return 0.00;
JSONObject jObject = new JSONObject(newsJSON); }
conversionValue = jObject.getDouble(currencyOne + "_" + currencyTwo);
try {
} catch (JSONException e) { JSONObject jObject = new JSONObject(newsJSON);
conversionValue = jObject.getDouble(currencyOne + "_" + currencyTwo);
Log.e("MainActivity", "Problem parsing the JSON results", e);
} } catch (JSONException e) {
return conversionValue;
} Log.e("MainActivity", "Problem parsing the JSON results", e);
}
private class MyAsyncTask extends AsyncTask<String, Void, Double> { return conversionValue;
}
@Override
protected Double doInBackground(String... urlString) { private class MyAsyncTask extends AsyncTask<String, Void, Double> {
String jsonResponse = null;
if (urlString.length < 1 || urlString[0] == null) { @Override
return null; protected Double doInBackground(String... urlString) {
} String jsonResponse = null;
try {
URL url = createUrl(urlString[0]); if (urlString.length < 1 || urlString[0] == null) {
jsonResponse = makeHttpRequest(url); return null;
}
} catch (IOException e) { try {
Log.e(LOG_TAG, "Problem making the HTTP request.", e); URL url = createUrl(urlString[0]);
} jsonResponse = makeHttpRequest(url);
} catch (IOException e) {
return extractFeatureFromJson(jsonResponse); Log.e(LOG_TAG, "Problem making the HTTP request.", e);
}
}
@Override return extractFeatureFromJson(jsonResponse);
protected void onPostExecute(Double result) {
super.onPostExecute(result); }
conversionRateOne = result; @Override
protected void onPreExecute() {
} super.onPreExecute();
spinner.setVisibility(View.VISIBLE);
} }
@Override
protected void onPostExecute(Double result) {
super.onPostExecute(result);
conversionRateOne = result;
spinner.setVisibility(View.GONE);
}
}
} }