I previously developed a real time currency converter program, and would like to export it as an Android app.
To do so, I am using a GET request with RestTemplate to get the rate between 2 currencies. However, I then get this error: android.os.NetworkOnMainThreadException
Here is the code I first tried:
public void displayResult(View view) {
TextView output = (TextView) findViewById(R.id.conversion);
output.setInputType(InputType.TYPE_CLASS_NUMBER);
output.setText(String.valueOf(convert()));
}
float convert() {
EditText textBaseCurrency = (EditText) findViewById(R.id.BASE_CURRENCY);
EditText textTargetCurrency = (EditText) findViewById(R.id.TARGET_CURRENCY);
EditText textAmount = (EditText) findViewById(R.id.AMOUNT);
String baseCurrency = textBaseCurrency.getText().toString().toUpperCase();
String targetCurrency = textTargetCurrency.getText().toString().toUpperCase();
float amount = Integer.parseInt(textAmount.getText().toString());
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
String url = "https://v6.exchangerate-api.com/v6/" + API_KEY + "/latest/" + baseCurrency;
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
System.out.println(responseEntity);
ObjectMapper mapper = new ObjectMapper();
JsonNode root = null;
try {
root = mapper.readTree(responseEntity.getBody());
} catch (IOException e) {
e.printStackTrace();
}
JsonNode name = root.get(conversionRates);
JsonNode rates = name.get(targetCurrency);
float rate = rates.floatValue();
float newAmount = rate * amount;
return newAmount;
}
I saw that the solution would be to use AsyncTask
. I tried it, but I wasn't successful:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Connection().execute();
}
public class Connection extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
EditText textBaseCurrency = (EditText) findViewById(R.id.BASE_CURRENCY);
EditText textTargetCurrency = (EditText) findViewById(R.id.TARGET_CURRENCY);
EditText textAmount = (EditText) findViewById(R.id.AMOUNT);
String baseCurrency = textBaseCurrency.getText().toString().toUpperCase();
String targetCurrency = textTargetCurrency.getText().toString().toUpperCase();
float amount = Integer.parseInt(textAmount.getText().toString());
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
String url = "https://v6.exchangerate-api.com/v6/" + API_KEY + "/latest/" + baseCurrency;
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
System.out.println(responseEntity);
ObjectMapper mapper = new ObjectMapper();
JsonNode root = null;
try {
root = mapper.readTree(responseEntity.getBody());
} catch (IOException e) {
e.printStackTrace();
}
JsonNode name = root.get(conversionRates);
JsonNode rates = name.get(targetCurrency);
float rate = rates.floatValue();
float newAmount = rate * amount;
return null;
}
Moreover, this is now deprecated.
My gradle.build file:
packagingOptions{
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
}dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.9.10.6'
implementation 'javax.xml.stream:stax-api:1.0'
implementation 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
}
I also added <uses-permission android:name="android.permission.INTERNET" />
in my AndroidManifest.xml